In JavaScript, there are two top-level objects Function and Object. Object is the base class of all objects, and all constructor functions are instances of the Function object at the same time.
All objects in JavaScript are derived from Object. All objects inherit methods and properties from Object.prototype, although they may be overridden, for example, other constructor functions implement their own toString() method in the prototype. Changes to the Object prototype object will propagate to all objects unless these overridden properties and methods along the prototype chain are modified.
// Define three objectsvara=function(){}// constructor function objectvar b =newArray(1);// array objectvar c =newNumber(1);// number object // wrapper object// Check the prototype chainconsole.log(a.__proto__.__proto__ ===Object.prototype);// trueconsole.log(b.__proto__.__proto__ ===Object.prototype);// trueconsole.log(c.__proto__.__proto__ ===Object.prototype);// true// Split the referencesconsole.log(a.__proto__ ===Function.prototype);// trueconsole.log(Function.prototype.__proto__ ===Object.prototype);// trueconsole.log(b.__proto__ ===Array.prototype);// trueconsole.log(Array.prototype.__proto__ ===Object.prototype);// trueconsole.log(c.__proto__ ===Number.prototype);// trueconsole.log(Number.prototype.__proto__ ===Object.prototype);// true// Using instanceof is actually checking the prototype chain// The instanceof operator checks whether the prototype property of a constructor appears in the prototype chain of an instance objectconsole.log(a instanceofObject);// trueconsole.log(b instanceofObject);// trueconsole.log(c instanceofObject);// true
All constructor functions in JavaScript inherit from Function, including the Object constructor function. The Function constructor function also inherits from itself, and of course, Function also inherits from Object.prototype, it can be said that the Object.prototype existed first, and Object.prototype constructed Function.prototype, and then Function.prototype constructed Object and Function.
// Constructor function objectvara=function(){}// constructor function object// Check the prototype chainconsole.log(a.__proto__ ===Function.prototype);// trueconsole.log(Object.__proto__ ===Function.prototype);// trueconsole.log(Function.__proto__ ===Function.prototype);// trueconsole.log(Function.prototype.__proto__ ===Object.prototype);// true// Using instanceof console.log(a instanceofFunction);// trueconsole.log(Object instanceofFunction);// trueconsole.log(Function instanceofFunction);// true