// Objects with prototype are plain iff they were constructed by a global Object function
Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor; //这行是什么意思?
return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
以下是上訴代碼中提到的代號
var class2type = {};
var toString = class2type.toString;
var getProto = Object.getPrototypeOf;
var hasOwn = class2type.hasOwnProperty;
var fnToString = hasOwn.toString;
var ObjectFunctionString = fnToString.call( Object );
var proto, Ctor;
proto = getProto( obj );
在 jQuery 3.2.1 中,判定物件是否為純物件的函數,最後兩行,我自己測試的時候是不對等的,想問下問題在哪裡
var obj = function(){};
var proto = Object.getPrototypeOf(obj);
var Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
var objHasOwn = Object.hasOwnProperty.toString.call( Object );
var funcHasOwn = Object.hasOwnProperty.toString.call( Ctor );
console.log(funcHasOwn === objHasOwn); // 我使用纯 function 来测试,发现结果是 false
問下
為什麼輸出結果是 false
#Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
是什麼意思
#Object.hasOwnProperty.toString.call( Object )
輸出為function Object() { [native code] }
Object.hasOwnProperty. toString.call( Number )
輸出為function Number() { [native code] }
是為什麼
1、var obj = function(){};回傳false是因為你對這個函數功能理解錯了,它不是說透過函數創建,它的意思是new Object();
Object.hasOwnProperty.toString.call( Number )輸出為function Number() { [native code] }是因為Object.hasOwnProperty.toString會以字串方式傳回該物件的建構子函數,call方法借用Object.hasOwnProperty上的toString方法。