Object-oriented learning of JavaScript nine (prototype inheritance and parasitic inheritance)

Source: Internet
Author: User

First, the prototype inheritance

The inheritance model was proposed by Douglas Rockford in 2006 to implement the method of inheritance.

The basic idea of a pattern: a prototype allows you to create new objects based on existing objects without having to create custom types.

The code is as follows:

functionObject (o) {functionF () {//defines an object of type F         This. Name= "111"; } F.prototype=o;//causes the prototype object of F to point to the incoming object, that is, F inherits the incoming object, or the prototype object that overrides F with the passed object is equivalent to the following code/*f.prototype={Name: "Zhang San", friends:["John Doe", "Harry"]};*/    return NewF ();//return F Object Note: There is a constructor in the prototype object that points to the F constructor, so this object includes both the F prototype object and the properties inside the F constructor.}varperson={name:"Zhang San", friends:["John Doe", "Harry"]};varperson1=object (person); Person1.name= "Plugin"; Person1.friends.push ("Zhao Liu");varPerson2=object (person); Person2.name= "Kobe"; Person2.friends.push ("Durrant"); alert (person1.friends); //output: John Doe, Harry, Zhao Liu, Durrant


This inheritance pattern is similar to the prototype constructor pattern, except that here the F constructor acts as a base object, and his prototype object is dynamically changing according to the incoming object! So according to this feature, by changing the property values of the incoming object, without the steps to create the constructor, so when we do not have to create a constructor selectmen, just want to keep one object similar to another object, the prototype inheritance is fully capable!

Object-oriented learning of JavaScript nine (prototype inheritance and parasitic inheritance)

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.