JS in __proto__ and prototype the difference and relationship?

Source: Internet
Author: User


_proto__ (implicit prototypes) and prototype (explicit prototypes)

1. is what
    • An explicit prototype explicit prototype Property:
Each function will have a property named prototype after it is created, which points to the prototype object of the Function.
Note: The function constructed by the Function.prototype.bind method is an exception, and it does not have a prototype Property. (thanks @ Chen Yuru's answer let me know This)
NOTE Function objects created using Function.prototype.bind does not has a prototype property or the [[Code]], [[Form alparameters]], and [[Scope]] Internal Properties. -----ECMAScript Language Specification
    • Implicit prototype implicit prototype Link:
Any object in JavaScript has a built-in property [[prototype]] and there is no standard way to access this built-in property before ES5, but most browsers support access through __proto__. ES5 has a Get method object.getprototypeof () for this built-in property Standard.
Note:Object.prototype This object is an exception, its __proto__ value is null
    • The relationship between the Two:
Implicit prototype pointing CreateThe prototype of this Object's function (constructor)

2. What is the role?
    • The role of explicit prototypes: used to implement prototype-based inheritance and attribute Sharing.
ECMAScript does not with classes such as those in C + +, Smalltalk, or Java. Instead objects May is created in various ways including via a literal notation or via constructors which create objects a nd then execute code, initialises all or part of them by assigning initial the values to their properties. Each constructor are a function that have a property named "prototype" That's used to implement prototype-based Inher Itance and Gkfx Properties. Objects is created by using the constructors in new expressions; For example, new Date (2009,11) creates a new date Object. ----ECMAScript Language Specification
    • The role of implicit prototyping is to form a prototype chain, which is also used to implement prototype-based Inheritance. For example, when we visit the X attribute in obj, if it is not found in obj, it will be searched along the __proto__.
every object created by a constructor have an implicit reference (called the Object's Prototype) to the value of its Constructor ' s "prototype"----ECMAScript Language specification

3. __proto__ 's direction
How do you judge __proto__ 's point? According to the ECMA definition ' to the value of its constructor ' s "prototype"----point to the explicit prototype of the function that created the Object. So the key point is to find the constructor to create this object, then take a look at how the object is created in js, there seems to be three ways in the Past: (1) the way the object literal (2) New Way (3) ES5 in the Object.create () But I think there is essentially only one way, which is to create through NEW. Why do you say that? the first literal way is a syntactic sugar that is more convenient for developers to create objects, essentially var o = new Object (); o.xx = xx;o.yy=yy; Take a look at Object.create (), which is the new method in ES5, which was formerly known as prototype inheritance,
douglas
wrote an article in 2006 titled Prototypal Inheritance in JavaScript. In this article, he describes a method of implementing inheritance that does not use a strictly constructed Constructor. His idea was to use prototypes to create new objects based on existing objects, and not to create custom types, for this purpose, he gave the following functions:
function object(o){ function F(){} F.prototype = o; return new F()}
-----"javascript Advanced programming" P169

So from the implementation code return new F () We can see that this is still created by NEW. The difference is that the object created by Object.create () does not have a constructor, see here you are not to ask, no constructor how do I know where its __proto__ point, in fact, It is said that no constructor refers to the Object.create () Outside of the function we cannot access its constructor, but there is some in the inner implementation of the function, it briefly exists for a moment. Assuming we're inside the function now, we can see that the Object's constructor is f, and now
//以下是用于验证的伪代码var f = new F(); //于是有f.__proto__ === F.prototype //true//又因为F.prototype === o;//true//所以f.__proto__ === o;
So the object created by Object.create (o) has its implicit prototype pointing to O. well, the way the object is created, you should now be able to tell who the __proto__ of an object is pointing to.

well, Let's take a look at some examples of past doubts to Consolidate.

    • Implicit prototype of the display prototype of the Constructor:
    1. Built-in objects (built-in object): such as Array (), what does array.prototype.__proto__ point to? Array.prototype is also an object, which is created by the constructor of object (), so array.prototype.__proto__ = = = object.prototype//true, or it can be understood, All built-in objects are created by object ().
    • Custom Objects
1. by default:
function Foo(){}var foo = new Foo()Foo.prototype.__proto__ === Object.prototype //true 理由同上
2. Other situations:
(1)
function Bar(){}//这时我们想让Foo继承BarFoo.prototype = new Bar() Foo.prototype.__proto__ === Bar.prototype //true
(2)
//我们不想让Foo继承谁,但是我们要自己重新定义Foo.prototypeFoo.prototype = { a:10, b:-10}//这种方式就是用了对象字面量的方式来创建一个对象,根据前文所述 Foo.prototype.__proto__ === Object.prototype
Note: The above two cases are equivalent to completely rewrite the foo.prototype, so Foo.prototype.constructor also changed, so constructor this attribute and the original constructor Foo () also cut off the Contact.

    • An implicit prototype of a constructor
Since it is a constructor then it is an instance of function () and therefore points to function.prototype, such as object.__proto__ = = = Function.prototype

4. instanceof
The internal implementation mechanism of the instanceof operator is directly related to the implicit prototype and explicit Prototype. The left value of a instanceof is generally an object, and the right value is typically a constructor that is used to determine whether an lvalue is an instance of the right-hand value. The principle of its internal implementation is this:
//设 L instanceof R //通过判断 L.__proto__.__proto__ ..... === R.prototype ?//最终返回true or false
That is, the __proto__ along L have been looking for the end of the prototype chain until it is equal to R.prototype. Knowing this, I know why the following strange expressions are worth the Value.
Function instanceof Object // true  Object instanceof Function // true Function instanceof Function //true Object instanceof Object // true Number instanceof Number //false

Article reference: JavaScript instanceof operator In-depth anatomy


 

JS in __proto__ and prototype the difference and relationship?

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.