Article Introduction: JavaScript is object-oriented, and all objects have a prototype chain of their own. object and function may look at the object instanceof function, the function instanceof object are all true, so first look at the instance of the objects. |
JavaScript is object-oriented, and all objects have a prototype chain of their own. object and function may look at the object instanceof function, the function instanceof object are all true, so first look at the instance of the objects.
One, JS in the so-called example
1. such as var a = new A (); It is generally considered that "A is an instance object of a function".
2. What is the process of the new operation?
1.new Create an empty object {} called small C
2. Then place the A.prototype at the top of the prototype chain of small c. Small c.__proto__ Pointing A.prototype
3. Perform the A function, point A to small C, execute the end, if there is no return then the default returns this reference.
Then one of the roles of new is to add A.prototype to the prototype chain of a.
3. instanceof for JS to judge the example of the method, the basis of judgment is (example a instanceof a) to determine whether a prototype chain contains A.prototype.
4. The general knowledge of new examples is included. The so-called O is the example object of FN, actually refers to the O prototype chain contains Fn.prototype
Two, object and function of the fuzzy diagram (oneself draw very rub)
Explanation: All the prototype objects of the functions have constructor, prototype properties. All objects have a prototype chain (Object.prototype's prototype chain is null)
After reading this picture, you should understand why instanceof is the result of that.
The relationship between objects and object, the prototype chain ends in Object.prototype
Object.prototype's prototype chain is empty (that is, there is no prototype object). Therefore Object.prototype instanceof Object is false.
Judge type with typeof, typeof (Object.prototype) = = ' object '. So not all objects are instances of object objects. All objects except Object.prototype should be instances of object object.