To better understand this article, please learn the basic knowledge of prototype and prototype chain in advance.
Let's take a look at the following questions about the pitfalls:
1 Object.__proto__ == Object.prototype2 Function.__proto__ == Function.prototype3 Object instanceof Function4 Function instanceof Object
A lot of people may be confused at first glance (including the author). It feels a bit like an exam-oriented education question. Don't worry. Let's take a look at the common code first,Var a = new ();We all know that it was created in the memory.AAn instanceA, In the process of creating this instanceNewWhat did the keyword do?
1 var a ={}; // create an empty object 2. _ proto _ =. prototype; // make the _ proto _ attribute of a reference to the prototype attribute of. add prototype to prototype chain a 3. call ();
HereAYesAAn instance,AThe prototype chain containsA. prototype. So we will continue to extendJavascript, Any object isObjectObject instance, so4. Function instanceof ObjecT isTrue.
In particular,JavascriptAndFunction ()This wonderful object, anyFunction ()The Type constructor or function is its instance. We are executingTypeof (Object)Actually getFunctionAnd can be viewed
1 function Object() = {};
Like a constructor ?! So we canVar obj = new Object ();This descriptionObjectYesFunctionAn instance,3. Object instanceof FunctionIt is also true.
Let's talk aboutInstaceof, IfA instaceof BReturnTrue, DescriptionB. prototypeInABut is not equivalentA. _ proto _ = B. prototypeIsTrue. ForFunctionAndObjectThis is a wonderful combination.
1 Object.__proto__ == Function.prototype;2 Function.__proto__ == Function.prototype;3 Function.__proto__.__proto__ == Object.prototype;
So1. Object. _ proto _ = Object. prototypeIsFalse,2. Function. _ proto _ = Function. prototypeIsTrue.
Function is a special Object.
See http://www.cnblogs.com/zzcflying/archive/2012/07/20/2601112.html for details