Prototype and Inheritance of JavaScript objects

Source: Internet
Author: User

Each JavaScript Object contains an internal reference to a prototype object. Any attribute of the prototype object is represented as the attribute of each method with its prototype. That is to say, JavaScript objects can inherit attributes from their prototype.

Prototype implementation principle

All objects can have prototype. prototype itself is also an object, so it can also have prototype. In this way, a prototype chain is formed after a loop, this chain is terminated when prototype in the formation in the chain is null. (The default prototype of the object is null ).

1 var objectref = new object (); // create a generic JavaScript Object.

 

Create a new JS object. The prototype of this object is null, so the prototype chain of objectref contains only one object. prototype.
Let's look at the following code:

1/* construct the constructor myobject1-type.
2 */
3 function myobject1 (formalparameter ){
4/* Create an attribute named testnumber for the person object */
5 This. testnumber = formalparameter;
6}
7
8/* construct the constructor myobject2-type :-*/
9 function myobject2 (formalparameter ){
10/* Create an attribute named teststring for the object owner */
11 This. teststring = formalparameter;
12}
13
14/* the next step will replace the default prototype attribute of myobject2 with the myobject1 object */
15 myobject2.prototype = new myobject1 (8 );
16
17/* Finally, create an object of the myobject2 type */
18 var objectref = new myobject2 ("string_value ");

 

The objectref object of the myobject2 type has a prototype chain. The first object in the chain is the myobject1 object, and the myobject1 object also has prototype. This prototype is the default prototype of the object. prototype is null. This prototype chain ends.

When a value operation occurs, the entire prototype chain of objectref starts to work.

1 var val = objectref. teststring;

 

The objectref object has a property named teststring, so this code will assign the value of teststring to Val.

 

1 var val = objectref. testnumber;

 

The object objectref does not have the attribute testnumber, but Val has a value of 8 rather than undefine. This is because the interpreter does not find the attribute to be found on the current object, check the prototype of the object. The prototype of objectref is the myobject1 object. This object has the property testnumber, so Val gets the value 8.

 

1 var val = objectref. tostring;

 

Now Val is a function reference. This function is the property of object. Prototype. Because neither myobject1 nor myobject2 defines the property tostring, object. prototype is returned.

 

1 var val = objectref. madeupproperty;

 

The final Val is undefined. Because neither myobject1 nor myobject2, nor the object has the property madeupproperty defined, the undefine is obtained.

 

The read operation reads the first attribute value with the same name found on the OBJ itself and prototype chain.
The write operation creates an attribute with the same name for the OBJ object itself (if this attribute name does not exist. This means objectref. testnumber = 3 creates a property on the objectref object named testnumber. Currently, the propertype chain does not work when you want to read testnumber, and only the objectref property 3 is obtained, the testnumber attribute of myobject1 is not modified.

 

Method for Determining object prototype isprototypeof ()

If the isprototypeof () method belongs to the prototype object of the parameter, the method returns true. Otherwise, false is returned. For example:

1 var o = {};
2 object. Prototype. isprototypeof (o); // true: O. constructor = object;
3 object. isprototypeof (o); // false
4 O. isprototypeof (object. Prototype); // false
5 function. Prototype. isprototypeof (object); // true: object. constructor = Function

 

 

Determine whether the inheritance attribute of the prototype object is

The object. hasownproperty () method is used to distinguish inherited attributes from object attributes.

 

Related Articles: This keyword for Javascript

 

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.