Inheritance-javascript Object-oriented advanced

Source: Internet
Author: User

# # of Inheritance # #
JS is not a completely object-oriented language, it does not specifically implement the mechanism of object inheritance, but can be implemented through the syntax of the object inheritance, to achieve inheritance can be implemented in several ways to achieve inheritance.
(This can also be inherited by the class syntax in ES6, closer to the traditional language such as Java, see my ES6 new feature)

2. Pass the value through constructor.

3. Classes Class Concept: constructors

An object produced by a constructor is called an instance. All properties that have a constructor function
Extract common methods and properties and save them to the prototype


Several ways of inheriting
1. Inheriting through prototypes
function Father () {
this.name = ' Zhangsan ';
This.age = 12;
This.gender = ' mail ';
This.sayhello = function () {
Console.log (' hello,i m ' +this.name + ', i m ' +this.age+ ' years old ')
}
}
function child () {};

Child.prototype = new Father ();
var C1 = new Child ();
C1.sayhello ()


2. prototype inheritance of pass parameters

function Father (name,age,gender) {
THIS.name = name;
This.age = age;
This.gender = gender;
}
Defining the Parent Object
Father.prototype.sayHello = function () {
Console.log (' hello,i m ' +this.name + ', i m ' +this.age+ ' years old ')
}

function Child (Name,age,gender) {
This.constructor (Name,age,gender)
Passing parameters to constructor
};

Child.prototype = new Father ();
var C1 = new Child (' Zhangsan ', Max, ' FEMAIL ');
C1.sayhello ()

* * Problem: The method is a prototype-overridden method that inherits all the properties of the parent element, but if the parent element does not have a prototype attribute specific to some child elements. The property will be lost. **

3. Object Impersonation
A constructor is called as a normal function and is then generated to point to the current object

function Father (name,age,gender) {
THIS.name = name;
This.age = age;
This.gender = gender;
}

Father.prototype.sayHello = function () {
Console.log (' hello,i m ' +this.name + ', i m ' +this.age+ ' years old ')
}

function Child (Name,age,gender) {
This.newobj = father;//This sentence is the key
This.newobj (Name,age,gender)
Here, the Father function is treated as a normal function, and after execution, an object is created, which is the object impersonating
Delete This.newobj
};

var C1 = new Child (' lis ', +, ' FEMAIL ');
Console.log (C1.name)

* * Problem: This mode cannot inherit attributes on the Father prototype. **

4. Manually change the point of this by call and apply principle: (Object Impersonation)

function Father (name,age,gender) {
THIS.name = name;
This.age = age;
This.gender = gender;
}

Father.prototype.sayHello = function () {
Console.log (' hello,i m ' +this.name + ', i m ' +this.age+ ' years old ')
}

function Child (Name,age,gender) {
Father.call (This,name,age,gender)
This sentence is the key to the father all this is pointing to the child
};

var C1 = new Child (' Zhangsan ', Max, ' FEMAIL ');
C1.sayhello ()

* * Problem: Because the substance is the object impersonating, so also can not inherit father's prototype * *

5. Combined Inheritance (mixed)

function Father (name,age,gender) {
THIS.name = name;
This.age = age;
This.gender = gender;
}

Father.prototype.sayHello = function () {
Console.log (' hello,i m ' +this.name + ', i m ' +this.age+ ' years old ')
     }

     function Child (Name,age,gender) {
        father.call (this,name,age,gender)
Span style= "COLOR: #008000" >     };
       //not only does this use the object impersonation Father this constructor,
       //also inherits father's prototype

      child.prototype = Father.prototype;
     var c1 = new Child (' lis ', +, ' FEMAIL ');
     console.log (C1)
     c1.sayhello ()

Inheritance-javascript Object-oriented advanced

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.