Inheritance _javascript techniques in JavaScript

Source: Internet
Author: User

Introduction to Inheritance

Inheriting in JS is a very complex topic, more complex than any other object-oriented language inheritance. In most other object-oriented languages, it takes a single keyword to inherit a class. In JS to achieve the goal of inheriting public members, a series of measures need to be taken. JS is a prototype inheritance, and thanks to this flexibility, we can either use standard class-based inheritance or use more subtle prototypes for inheritance. In the JS should be clear, all inheritance is done through prototype, and JS is based on the object to inherit.

Inherited:

function Animal (name) { 
this.name = name; 
This.showname = function () { 
alert (this.name); 
} 
} 
function Cat (name) { 
Animal.call (this, name); 
} 
var cat = new Cat ("Black Cat"); 
Cat.showname ();

Animal.call (this) means to use the Animal object instead of this object, then the cat does not have all the Animal properties and methods, the Cat object can directly call the Animal method and properties.

Multiple inheritance:

function Class10 () 
{ 
this.showsub = function (a,b) 
{ 
alert (a-b); 
} 
} 
function Class11 () 
{ 
This.showadd = function (a,b) 
{ 
alert (a+b); 
} 
} 
function Class2 () 
{ 
class10.call (this); 
Class11.call (this); 
}

It's easy to use two call to achieve multiple inheritance.

Of course, JS inheritance There are other methods, such as the use of prototype chain, this does not belong to the scope of this article, but here to explain the use of call. Said call, and of course, apply, the two methods are basically a meaning, the difference is that the second argument of call can be any type, and the second parameter of apply must be an array, or it can be arguments.

Let's show you how to implement simple inheritance in JavaScript.

The following example creates an employee class employee that inherits all the attributes from the prototype prototype from person.

function Employee (name, sex, EmployeeID) {
this.name = name;
This.sex = sex;
This.employeeid = EmployeeID;
}
Point the employee's prototype to an instance of person
//Because instances of person can invoke methods in the person prototype, so an instance of employee can also invoke all attributes in the person prototype.
employee.prototype = new Person ();
Employee.prototype.getEmployeeID = function () {return
this.employeeid;
};
var Zhang = new Employee ("Zhangsan", "Man", "");
Console.log (Zhang.getname ()); "Zhangsan

The implementation of the above about inheritance is rough, and there are many problems:

It is not appropriate to instantiate a person when creating an employee constructor and prototype (hereafter referred to as a class).

The constructor of employee cannot invoke the constructor of the parent class person, causing a duplicate assignment of the name and sex properties in the employee constructor.

Functions in employee overwrite functions with the same name in person, without mechanisms for overloading (and the previous one is a type of problem).

The syntax for creating JavaScript classes is too fragmented to be as elegant as the syntax in C#/java.

The implementation contains a pointing error for the constructor property.

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.