A misunderstanding of JavaScript prototype chain and prototype _javascript tips

Source: Internet
Author: User

Before I was in the prototype chain of JavaScript, prototype inheritance and identifier lookup were somewhat confusing,

For example, the following code:

Copy Code code as follows:

function Foo () {};
var foo = new Foo ();
Foo.prototype.label = "Laruence";
alert (Foo.label); Output:laruence
alert (Foo.label);//output:undefined

Today we see the following figure:

Javascript Object Layout
Also, in JavaScript Object hierarchy see:

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

In other words, the prototype of the function object does not work in the prototype chain lookup process,

Today in Firefox found (because Firefox through the __proto__ exposed [[[prototype]]), the real participation identifier lookup is the function object's __proto__,

Copy Code code as follows:

function Foo () {};
var foo = new Foo ();
Foo.__proto__.label = "Laruence";
alert (Foo.label); Output:laruence
alert (Foo.label);//output:undefined

And, apparently:

Copy Code code as follows:

function Foo () {};
Alert (foo.__proto__ = = Foo.prototype); Output:false

In addition, it was explained that

Copy Code code 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

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.