Javascript prototype and prototype inheritance

Source: Internet
Author: User

Each javascript Object (except null) is associated with the prototype object, and each object inherits attributes from the prototype object. All objects created directly through objects have the same prototype Object, and can be referenced to the prototype Object through javascript code Object. prototype. The prototype of the object created by calling the keyword new and constructor is the value of the prototype attribute of the constructor. Therefore, like creating an Object using {}, objects created using new Object () are also inherited from Object. prototype. Similarly, the prototype of the object created through new Array () is Array. prototype. The prototype of the object created through new Date () is Date. prototype. First, let's take a look at the Object. create method. ECMAScript5 defines an Object. create () method, which creates a new object. The first parameter is the prototype of the object, and the second parameter is used to further describe the attributes of the object. It is easy to use, just pass in the required prototype object. For example, var o1 = Object. create ({a: 1, B: 2}); // o1 inherits the attributes x and y var o2 = {x: 10, y: 20}; var o3 = Object. create (o2); // o3 inherits o2. If you want to create a common Null Object (for example, an Object created through {} or new Object (), you must pass in the Object. prototype, for example, var o4 = Object. create (Object. prototype); // use Object like {} and new Object. the create method can create a new object through any prototype object, that is, any object can be inherited. Therefore, we can write a prototype inherited function, as shown below: function inherit (p) {if (Object. create) {return Object. create (p);} else {function f () {} f. prototype = p; return new f () ;}next, let's take a look at the inheritance of javascript objects. Javascript objects have "self-owned attributes", and some attributes are inherited from the prototype object. Assume that the property x of the object o is to be queried. If there is no x in o, the property x will be queried in the prototype object o. If the prototype object does not have x, but the prototype also has the original type, you can continue to execute the query on the prototype object to find x or find an object whose prototype is null. As you can see, the prototype property of an object constitutes a "chain", through which attributes can be inherited. For example: var o ={}; // o from Object. prototype inherits the method o of the object. x = 1; var p = inherit (o); // p inherits o and Object. prototype p. y = 2; var q = inherit (p); // q inherits p, o, and Object. prototype q. z = 3; q. x + q. y // 3: x and y inherit from o and p respectively.

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.