JavaScript object, prototype chain

Source: Internet
Author: User

Object

ECMAScript is a highly abstract object-oriented language that handles objects. There are also basic types, but they can also be transformed into objects if they are needed.

Definition: An object is a series of properties and a unique prototype object. The prototype object may be an object, or it may be empty.

Let's look at an example of a basic object. The prototype of the object is referenced by an internal [[Prototype]]. However, we will be using __<internal_property>__

The lower line symbol replaces the double bracket, then for the prototype object: __proto__.

For the following code:

var foo = {   ten,   y: $     };

Our structure has two explicit attributes and an internal __proto__ property, and the __proto__ property is a reference to the prototype of Foo.

So what's the use of these attributes? Let's take a look at the prototype chain to answer this question.

Prototype chain

Prototype objects are simply objects, and perhaps have their own properties. If a prototype references a non-empty object to its prototype attribute, and so on, this is called the prototype chain.

The prototype chain is a finite chain of objects, and it is used to implement inheritance and share attributes.

Consider that when we have two objects, they are only a small part different and the others are the same. Obviously, for a good system design, we naturally want to be able to reuse similar functions/code,

Instead of repeating this section of functionality/code in each single object. In a class-based system, this repeating code uses the called Class inheritance. Integrate similar functions into Class A, providing class b,c to inherit from

Class A, so that each of them has an additional difference.

The ECMAScript language does not have a class concept. However, code reuse is not much different (even in some ways, it is more extensible than a class language) and is implemented through a prototype chain.

This inheritance is called Delegate inheritance (closer to ECMAScript, based on prototype inheritance).

Similar examples of Class a,b,c, in ECMAScript you can create objects: A, B, and C. In this way, the A object holds the same part as the object b,c. Objects b,c are stored in their

The respective properties or methods.

 var  a = {x:  10, calculate:  function   (z) { return  this . Y + Z; }}; var  b = {y:  20, __proto__: a};  var  C = {y:  30, __proto__:a}b.calculate ( 30); //  c.calculate (40); // 80  

It's simple, isn't it? We see that the object B,c can access the Calculate method, which is defined in the A object. This is achieved through the prototype fateful.

This simple rule: if a property or method is not found in its own object (for example, the object itself does not have this property), it will be in the prototype chain

Attempt to access this property or method. If it is not found in the prototype object, then it is searched in the prototype object of the prototype object, and so on, to the whole prototype chain (

Similar to a class chain). The first time you find it, it will return. In this way, the found property is called an inheritance property. If you look up the entire prototype chain and don't find the property, then

Returns undefined.

Notice that the value of this is set in the inherited method to the original object, not the prototype object, which is found in the object. For example, the THIS.Y is from

B and C, not from a. However, This.x is taken from a through the prototype chain mechanism.

If the prototype attribute does not explicitly specify an object, the default __proto__ value is Object.prototype. The Object.prototype of object also has a __proto__, this is the end of the chain,

and is set to empty.

The following picture illustrates the inheritance hierarchy about our object a,b,c:

JavaScript object, prototype chain

Related Article

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.