A misunderstanding of Javascript prototype chain and prototype, a misunderstanding of javascript prototype

Source: Internet
Author: User

A misunderstanding of Javascript prototype chain and prototype, a misunderstanding of javascript prototype

Previously, I was confused about prototype inheritance and identifier search in the prototype chain of Javascript,

For example, the following code:

Copy codeThe Code is as follows:
Function Foo (){};
Var foo = new Foo ();
Foo. prototype. label = "laruence ";
Alert (foo. label); // output: laruence
Alert (Foo. label); // output: undefined

The following figure is displayed today:

Javascript object layout
In addition, you can see in Javascript Object Hierarchy:

The prototype is only used for properties inherited by objects/instances created by that function. The function itself does not use the associated prototype.

That is to say, the prototype of the function object does not apply to the search process of the prototype chain,

Today, I found in firefox that (because firefox exposes [[prototype] Through _ proto _), the _ proto __,

Copy codeThe Code is as follows:
Function Foo (){};
Var foo = new Foo ();
Foo. _ proto _. label = "laruence ";
Alert (Foo. label); // output: laruence
Alert (foo. label); // output: undefined

Obviously:

Copy codeThe Code is as follows:
Function Foo (){};
Alert (Foo. _ proto _ = Foo. prototype); // output: false

In addition, it also explains,

Copy codeThe Code is as follows:
Alert (Object. forEach); // undefined
 
Function. prototype. forEach = function (object, block, context ){
For (var key in object ){
If (typeof this. prototype [key] = "undefined "){
Block. call (context, object [key], key, object );
}
}
 
};
 
Alert (Object. forEach );
Alert (Function. forEach );
Alert (Object. forEach === Function. forEach); // true


Javascript prototype link help

You need to distinguish between a function and an object instance. A function has two attributes: prototype and _ proto __. an object instance has only the _ proto _ attribute, __proto _ properties are hidden and inaccessible, but they are directly accessed in the Chinese Emy of Sciences (advanced browser), such as Google. _ Proto _ is the real prototype chain pointer. prototype is only the prototype object of the specified function. A. prototype is a prototype object. It does not have the prototype attribute. You can change it to console. log (. prototype. _ proto __. name); it will be displayed in the console when running in Google.

Is the javascript scope chain related to the prototype chain? From the principle analysis, do not say what I found online

In my personal opinion, there is no contact.
In general, the scope chain is for variables. For a large range in js, there are only two scopes: global scope and internal function scope, if function 2 is defined in function 1 (usually anonymous), a global scope of function 1 is available; the feature is that the global scope variables can be used directly in function 1, and the global scope and function 1 Scope variables can be used directly in function 2.
The prototype chain is generally used to define constructors. It can be considered to be for constructors or classes corresponding to constructors. the header of the prototype chain is the Object class/constructor, if there is a constructor 1. prototype = constructor 2; then there is such a prototype chain; Object => constructor 1 => constructor 2, the advantage is that constructor 2 corresponds to a class, you can have attributes in constructor 1 and Object. js does not have the corresponding inherited keywords. Therefore, you can use the prototype chain to simulate the inheritance effect.
I hope it will help you

Related Article

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.