Implementation of JS Inheritance

Source: Internet
Author: User

Considerations in Inheritance Link http://www.cnblogs.com/liyatang/archive/2011/05/30/2062611.html

There are 5 ways to implement JS inheritance:
1, inherit the first way: Object posing
function Parent (username) {
This.username = Username;
This.hello = function () {
alert (this.username);
}
}
function Child (Username,password) {
Inheritance is implemented by appending the properties and methods of the parent to child in the following 3 rows
The first step: This.method is as a temporary property and points to the object that the parent points to,
Step two: Execute the This.method method, which is the object function that the parent points to
Step three: Destroy the This.method attribute, which means that now child has all the properties and methods of the parent
This.method = Parent;
This.method (username);//the most critical line
Delete This.method;

This.password = password;
This.world = function () {
alert (This.password);
}
}
var parent = new Parent ("Zhangsan");
var child = new Child ("Lisi", "123456");
Parent.hello ();
Child.hello ();
Child.world ();

2, Inherit the second way: Call () method mode
The call method is a method in a function class
The value of the first parameter of the call method is assigned to the this that appears in the class (that is, a method)
The second argument of the call method begins by assigning the parameter to the class (that is, the method) that is accepted

function Test (str) {
Alert (THIS.name + "" + str);
}
var object = new Object ();
Object.name = "Zhangsan";
Test.call (Object, "Langsin");//At this point, the first parameter value object is passed to this in the test class (that is, the method), and the second parameter "Langsin" is assigned to the test class (that is, the method) str

function Parent (username) {
This.username = Username;
This.hello = function () {
alert (this.username);
}
}
function Child (Username,password) {
Parent.call (This,username);

This.password = password;
This.world = function () {
alert (This.password);
}
}
var parent = new Parent ("Zhangsan");
var child = new Child ("Lisi", "123456");
Parent.hello ();
Child.hello ();
Child.world ();

3, the third way of inheritance: Apply () method mode
The Apply method accepts 2 parameters,
A, the first parameter is the same as the first parameter of the call method, which is the value that appears in the class (that is, the method).
B, the second parameter is an array type, and each element in the arrays is assigned to a parameter that is accepted by the class (that is, the method)

function Parent (username) {
This.username = Username;
This.hello = function () {
alert (this.username);
}
}
function Child (Username,password) {
Parent.apply (this,new Array (username));

This.password = password;
This.world = function () {
alert (This.password);
}
}
var parent = new Parent ("Zhangsan");
var child = new Child ("Lisi", "123456");
Parent.hello ();
Child.hello ();
Child.world ();

4, inheritance of the fourth way: The prototype chain, that is, the subclass by prototype all the properties and methods appended to the parent class through prototype are appended to child, thus implementing the inheritance
function person () {
}
Person.prototype.hello = "Hello";
Person.prototype.sayHello = function () {
alert (This.hello);
}

function Child () {
}
Child.prototype = new Person (),//The function of this line is to append all the properties and methods appended by prototype to child in the parent, thus implementing the inheritance
Child.prototype.world = "World";
Child.prototype.sayWorld = function () {
alert (This.world);
}

var C = new Child ();
C.sayhello ();
C.sayworld ();

5, the fifth way of inheritance: Mixed mode
Hybrid call mode, prototype chain mode

function Parent (hello) {
This.hello = Hello;
}
Parent.prototype.sayHello = function () {
alert (This.hello);
}

function Child (Hello,world) {
Parent.call (This,hello);//Inherit the properties of the parent class.
This.world = world;//Added some properties
}

Child.prototype = new parent ();//Inherit the method of the parent class.

Child.prototype.sayWorld = function () {//New method
alert (This.world);
}

var C = new Child ("Zhangsan", "Lisi");
C.sayhello ();
C.sayworld ();

Implementation of JS Inheritance

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.