The prototype in JavaScript

Source: Internet
Author: User

Everything in JavaScript is an object, and many objects have prototype this member.

To understand this value of the opportunity or to interview the front-end, the interviewer asked me you know how to inherit JS, I face confused force. Now think about it is too little to know, even the introduction is not.

I tried it first. What is the prototype of the Origin object of everything?

Console.log (Object.prototye);

 

As a result, some of these functions are still well-acquainted, such as valueof (), toString ().

What I now know is that the prototype is useful for initializing the members of an object when new is created.

For example

function A () {}a.prototype = {    foo:function () {        return 123;    }};  var B = new A (); Console.log (B.foo ()); "123"

The above operation is equivalent to using the A function to initialize the B object, at initialization time the members inside the a.prototype copy to the B (Foo function).

At this point, if you look at B.prototype, you will return to undefined. Note B has no prototype this value. And a why does it have this value?

First look at what A.prototype looks like:

function A (ID) {
This.id=id;
}
Console.log (A.prototype);

The process of creating a function can actually be written like this:

var a=new Function ("id", "this.id=id");

So it is clear that a, created by the object of function, I guess, initializes the A.prototype within the function.

And here I stumbled upon the difference between these two methods of creating a function, and if you use the second one, that A.prototype is long:

var a=new Function ("id", "this.id=id"); Console.log (A.prototype);

The constructor in prototype points to an anonymous function, not a.

In fact, at initialization time, the A function itself is also called as a constructor function.

function A (ID) {    this.id = ID;} var B = new A ("Id1"); Console.log (b.id);    "Id1"

Prototype member constructor also deserves attention. This value is typically the owner of the prototype, or A.prototype.constructor=a.

function A (ID) {    this.id = ID;} var B = new A ("Id1"); Console.log (a.prototype.constructor);        "Function A (id) {this.id = ID;}" Console.log (b.constructor);    Ditto

Each object will have a value of __proto__, which will point to the prototype that created its own object.

function A (ID) {    this.id = ID;}  var B = new A ("Id1"); Console.log (b.__proto__); Display the contents of A.prototype

So follow this prototype chain to always find Object.prototype.

There are also two functions isprototypeof and hasownproperty briefly introduced:

isPrototypeOf checks whether an object is in the prototype chain of another object.

function A (ID) {    this.id = ID;} A.prototype.boo=3;var B = New A ("Id1"); Console.log (a.prototype.isprototypeof (b));    "True" Console.log (Object.prototype.isPrototypeOf (b));    "True"

hasOwnProperty checks whether a member is non-inherited.

function A (ID) {    this.id = ID;} A.prototype.boo = 3;var B = new A ("Id1"); Console.log (B.hasownproperty ("id"));   "True" Console.log (B.hasownproperty ("Boo"));    "False"

About the inheritance relationship in Java there are a lot of things to explore, I just know a little bit of fur, I hope in the future study can further understand the mystery.

The prototype in JavaScript

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.