In the course of learning javascript, the prototype problem has been puzzling me for a long time, when holding the famous red book, see the explanation of the prototype, always have Doubts.
After a lot of travel in the JavaScript world, the desire to study this part of the knowledge again, and read a lot of books and materials to __proto__ understand prototype the Concept.
So take this note, and later forget to come back to See.
If you look at the process of understanding some difficulties, the example in the code to run a run, try your hands may be able to solve a lot of doubts.
Everything is the object
As everyone knows, JavaScript in the world of objects, traced from a null
"everything is the object", this sentence is really a good marketing, easy to remember, easy to catchy, Impressive.
When all things are born, an null object, born out of thin air, and then Object , Function learning null to shape themselves, and they are married to each other, providing prototype and constructor , one for the offspring of the gene, one to create thousands of children and Grandchildren.
In javascript, null It also exists as an object, based on the descendants that it inherits, as the genus Object.
At first glance, it null 's like god, Object and Function Adam and Eve in the JavaScript world.
Prototype pointer
__proto__
In javascript, each object has a prototype object, and the internal pointer to the prototype object is the one from which __proto__ you can inherit the properties of the prototype object, which is the genetic link in javascript, so that you can know the ancestors of this Object. From the object, __proto__ you can access the prototype object that he Inherits.
var a = new Array();a.__proto__ === Array.prototype // true
In the above code, an instance of an array is created a , and the prototype of the instance points to it Array.prototype .
Array.prototypeitself is an object as well as an inherited prototype:
Object.prototype // true// 等同于 Array.prototype.__proto__ === Object.prototype
It is clear that the array itself is inherited from object, so who is the prototype of object pointing to?
null // true// 等同于 Object.prototype.__proto__ === null
Prototype.png
so, the object in JavaScript is traced from a null object. Buddha yue: All things are empty , good and Good.
In addition to using the method to .__proto__ access the prototype of an object, you can also use Object.getPrototypeOf methods to get the Object's prototype, and Object.setPrototypeOf to override the Object's prototype by means of the method
。
It is worth noting that, according to the language standard, the __proto__ property only needs to be deployed by the browser, the other environment can not have this attribute, and the front and back of the two underscore that it is essentially an internal property, should not be exposed to the User. As a result, you should use this attribute sparingly, Object.getPrototypeof and use and Object.setPrototypeOf to perform the read and write operations of the prototype Object.
__proto__attributes are used here to describe the prototype in the object, because it is more visually and easily understandable.
Prototype Object
prototype
function as a class citizen in javascript, which is both a function and an object, and the prototype of the function is pointing toFunction.prototype
var Foo = function() {}Foo.__proto__ === Function.prototype // true
function instances have properties in addition __proto__ to properties prototype .
A new instance object constructed from this function, whose prototype pointer __proto__ points to the Function's prototype Properties.
var a = new Foo();a.__proto__ === Foo.prototype; // true
The property of a function prototype , by itself, is a Object constructed instance Object.
Object.prototype; // true
prototypeproperty is special, It also has an implicit constructor , pointing to the constructor itself.
// truea.constructor === Foo; // truea.constructor === Foo.prototype.constructor; // true
Prototype2.png prototype chain concept
The basic idea of the prototype chain as the main method to implement inheritance is to use the prototype to let one reference type inherit the properties and methods of another reference Type.
Each constructor has a prototype object ( prototype ), and the prototype object contains a pointer to the constructor ( constructor ), and the instance contains an internal pointer () to the prototype object __proto__ .
so, If we make the prototype object equal to an instance of another type, the prototype object will contain a pointer to another prototype, and a pointer to another constructor is also included in the other Prototype. If another prototype is an instance of another type, the above relationship is still True.
In this way, the chain of examples and prototypes is constructed, which is the basic concept of the prototype chain .
Significance
The role of the "prototype chain" is that when reading a property of an object, the JavaScript engine first looks for the properties of the object itself, and if it cannot find it, it will go to its prototype and find it if it is not found. And so on, if the topmost object.prototype is not found, then the undefine is returned
Paternity test
In javascript, there are also ways to identify the relationship between parent and child Dna:
- instanceof
Operator returns a Boolean value that indicates whether an object was created by a Constructor.
- Object.isprototypeof ()
Isprotypeof returns True whenever an object is on the prototype chain
var Bar = function() {}var b = new Bar();b instanceof Bar // trueBar.prototype.isPrototypeOf(b) // trueObject.prototype.isPrototypeOf(Bar) // true
It is important to note that b the prototype of the instance is Bar.prototype notBar
A picture of a long history
Jsobj_full.jpg
This is a description of Object , Function and a function instance, the Foo connection between their prototypes. If you understand the above concept, this picture is not difficult to read.
From there, you can see an interesting place.
Function.prototype.__proto__Point Object.prototype to, This explanation Function.prototype is an Object instance, then should be first Object there again Function .
But Object.prototype.constructor.__proto__ it's pointing again Function.prototype . In this way, there is no Function , Object nor can I create an instance.
This produces a kind of "first chicken or egg first" Classic question, in the end is first Object or first? Function
So philosophical questions, left to you to think About.
I'm Just Feeling:
The more you explore the depths of javascript, the more philosophical the language is.
Wen/jc_huang (author of Jane's Book)
Original Link: http://www.jianshu.com/p/3bb6f208e459
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
The philosophical thought in JavaScript prototype