Inheritance introduction
Inheritance in JS is a very complex topic, much more complex than inheritance in any other object-oriented language. In most other object-oriented languages, inheriting a class requires only one keyword. In order to achieve the purpose of inheriting public members in JS, a series of measures need to be taken. JS is a prototype inheritance. Thanks to this flexibility, we can use standard class-based inheritance or more subtle prototype inheritance. It should be clear in JS that all inheritance is carried out through prototype, and JS is inherited based on objects.
inherit:
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 using Animal object instead of this object. Then don’t there be all the properties and methods of Animal in Cat? The Cat object can directly call Animal methods and properties.
Multiple inheritance:
function Class10() { this.showSub = function(a,b) { alert(ab); } } function Class11() { this.showAdd = function(a,b) { alert(a+b); } } function Class2() { Class10.call(this); Class11.call(this); }It's very simple, using two calls to achieve multiple inheritance
Of course, there are other methods for inheriting js, such as using prototype chains, which does not fall into the scope of this article, but only explains the usage of call here. Speaking of call, of course there is also apply. These two methods basically mean the same thing. The difference is that the second parameter of call can be of any type, while the second parameter of apply must be an array or arguments.
Here is a description of how to implement simple inheritance in JavaScript?
The following example will create an employee class Employee which inherits all properties in the prototype prototype from Person.
function Employee(name, sex, employeeID) {this.name = name;this.sex = sex;this.employeeID = employeeID;}// Point the Employee prototype to an instance of Person // Because the Person instance can call methods in the Person prototype, the Employee instance can also call all properties 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()); // "ZhangSanThe above implementation of inheritance is rough and there are many problems:
Person is instantiated when creating Employee constructor and prototype (hereinafter referred to as class), which is inappropriate.
Employee's constructor cannot call the constructor of the parent class Person, resulting in repeated assignments of name and sex attributes in the Employee constructor.
Functions in Employee will override functions of the same name in Person, without overloading mechanisms (and the previous one is the same type issue).
The syntax for creating JavaScript classes is too scattered and not as elegant as the syntax in C#/Java.
There is an error in the pointing of the constructor attribute in the implementation.