JavaScript Inheritance usage profiling _js Object oriented

Source: Internet
Author: User
Before delving into JavaScript inheritance, learn the following concepts:
Parent Class: Inherited class
Subclasses: Inherited classes
Super class: That is, the parent class
Abstract class: A class that is not typically instantiated, and its purpose is to inherit from other classes.
Base class: A class that is provided to other classes that can be inherited
Derived class: A class inherited from a base class

JavaScript object inheritance usually has the following 5 ways:
1. Object posing as
2.call () mode
3.apply () mode
4. Prototype chain
5. Blending mode

A. Objects posing as
The so-called object posing, is the new class to impersonate the old class (the old class must take the constructor method), so as to achieve the inheritance purpose.
Eg.1
Copy Code code as follows:

function people (Name,sex,age) {//Use constructor method
This.name=name;
This.sex=sex;
This.age=age;
This.say=function () {
Alert ("My name is" +this.name);
};

This.doing=function () {
Alert ("I am Speaking");
};
}
var marry=new people ("Marry", "Woman", "23");
Marry.say ();
Marry.doing ();

function White_people (name,sex,age) {
This.inherit=people;
This.inherit (Name,sex,age);
Delete This.inherit;

This.area=function () {
Alert ("I AM in Europe");
}
}
var tom=new white_people ("Tom", "Man", "21");
Tom.say ();
Tom.area ();
alert (tom.age);

In the example above, people is used to do the base class of the white_people, and remember that this format is used to impersonate an object that inherits its purpose.
This.inherit=people; Impersonating
This.inherit (Name,sex,age); Inherited
Delete This.inherit; Delete Inheritance
All new properties and new methods must again delete the inherited definition, in order to avoid overwriting related properties and methods of the parent class.
In addition, object impersonation supports multiple inheritance.
Eg.2
Copy Code code as follows:

function Worker (Pay,work) {
This.pay=pay;
This.work=work;
}
function City_worker (name,sex,age,pay,work) {
This.inherit=people;
This.inherit (Name,sex,age);
Delete This.inherit;

This.inherit=worker;
This.inherit (pay,work);
Delete This.inherit;
}

var jerry=new city_worker ("Jerry", "Man", "N", "$1000", "coder");
Jerry.say ();
alert (jerry.work);

Object impersonation has an insufficient place: when multiple inheritance mechanisms are implemented, if the base class has the same property or method, it inherits from the following class.

B.call () way
Just the encapsulated object impersonating a function. In this way, we no longer need to write a "classic" three words, but use the following sentence instead:
Base class. Call (object, parameter list)
Eg.1
Copy Code code as follows:

function Farmer (name,sex,age,pay,work) {
People.call (This,name,sex,age);
Worker.call (this,pay,work);
}

var nicholas=new farmer ("Nicholas", "Man", "", "$3000", "irrigator");
Nicholas.say ();
alert (Nicholas.pay);

Similarly, call () has a small problem with the properties and methods of the same name.

C.apply () way
Same as call (). Apply () is also a wrapper function for an object to impersonate. The format is:
Base class. Apply (object, array of parameters);
Eg.1
Copy Code code as follows:

function White_collar (name,sex,age,pay,work) {
People.apply (this,new Array (name,sex,age));
Worker.apply (This,[pay,work]);
}

var jiessie=new white_collar ("Jiessie", "Woman", "num", "$2500", "editor");
Jiessie.say ();
alert (jiessie.work);

Similarly, apply () has a small problem with the properties and methods of the same name.

D. Prototype chain
The above three methods are inherited by the constructor method, correspondingly, also have the prototype function way inheritance: the prototype chain.
Eg.1
Copy Code code as follows:

function Blue_collar () {
}
Blue_collar.prototype.name= "Jean";
Blue_collar.prototype.age= "33";
Blue_collar.prototype.say=function () {
Alert ("My name is" + this.name);
};

function City_blue_collar () {
}
City_blue_collar.prototype=new blue_collar ();

var jj=new city_blue_collar;
Jj.say ();

The prototype chain also has the disadvantage of a prototype chain: cannot pass parameters. In addition, the prototype chain does not support multiple inheritance because

E. Mixed mode
Use constructors to write the properties of a class, to inherit the attribute by call () or apply ()
A method of using a prototype to write, using a prototype chain to inherit the method
Eg.1
Copy Code code as follows:

Function Beauty (name,age) {
This.name=name;
This.age=age;
}
Beauty.prototype.say=function () {
alert ("daughter called" +this.name);
};

Function China_beauty (name,age,area) {
Beauty.call (this,name,age);
This.area=area;
}
China_beauty.prototype=new Beauty ();
China_beauty.prototype.from=function () {
Alert ("I am from" +this.area);
};

var diaochan=new china_beauty ("Sable Zen", "16", "Lintao");
Diaochan.say ();
Diaochan.from ();
Alert (diaochan.age);
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.