let proto = obj / / proto default
while (Object.getprototypeOf (proto)! = = null) {/ / determine whether the prototype object of proto exists
proto = Object.getprototypeOf(proto) // protoproto,
}
return Object.getPrototypeOf (obj) = proto* / / actually determines how many layers the prototype chain of obj has, and only one layer returns true
find the top of the prototype chain
when it comes to the inheritance of the prototype chain, you can understand it by running the code:
function Foo() {}
// objplain object
var obj = new Foo();
console.log(typeof obj, obj !== null);
let proto = obj
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}
// false
var isPlain = Object.getPrototypeOf(obj) === proto;
console.log(isPlain);
then the problem arises, according to the definition of the isPlainObject method on jQuery
isPlainObject: function( obj ) {
var proto, Ctor;
if ( !obj || toString.call( obj ) !== "[object Object]" ) {
return false;
}
proto = getProto( obj );
if ( !proto ) {
return true;
}
Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
}
var o = Object.create(null)
the object defined above uses $.isPlainObject to determine whether the result is true, which is the correct implementation according to redux's isPlainObject method to determine which result is false,.
Why not use Object.prototype.toString.call (obj)
to judge?
is to find the father
find null and then stop, and begin to compare the son of null with the father of the object of judgment.
object's father is the top object object, grandfather is null
father equals father
function father is function, grandfather is object, great grandfather is null
grandfather is not equal to father
find the top of the prototype chain
https://www.zhihu.com/questio.