Functions and objects in javascript, prototype and prototype chain

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.