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