Original Excerpt: https://www.cnblogs.com/gwf93/p/10384352.html
The first to write a parent class Car
function Car(name){ this.name = name; this.driver = function (){ console.log ( 'traveling thousands of miles' ) } } Car.prototype.addOil = function(param){ this.oil = param; the console.log ( the this .name + 'daily refueling' param + + 'L' ) }
(A) prototype inheritance chain:
Core: the instance of the parent class as a prototype subclass
instanceOf: a test object is an instance of a class
// prototype inheritance function baoma () { } BaoMa.prototype = new new key Car () // instance of the parent class as a prototype subclass inherits BaoMa.prototype.name = "BMW" var baoMa1 = new new baoma (); // instantiate subclass console.log(baoMa1.name) console.log(baoMa1.addOil(20)) console.log(baoMa1.driver()) console.log(baoMa1 instanceof Car) // true console.log(baoMa1 instanceof Car) // true
-
Features:
- Pure inheritance, instance is an instance of a subclass, as well as instances of the parent class
- Parent new prototyping / prototype property, the subclass can access to
- Simple, easy to implement
Disadvantages:
- Cat constructor may be increased for the instance attributes Cat instances. If you want to add the prototype properties and methods, it must be placed
new Animal()
executed after such statements. - Multiple inheritance can not be achieved
- All properties from the prototype object is shared by all instances
- When you create a subclass instance, can not pass parameters to the parent class constructor
(B) constructor Inheritance:
// constructor inherited function dazhong (name) { Car.call(this); this.name = name || '迈腾'; } maiTeng the let = new new dazhong (); console.log(maiTeng.name) // 迈腾 console.log(maiTeng.driver()) console.log(maiTeng instanceof Car); // false console.log(maiTeng instanceof DaZhong) // true
Features:
- Solved, the subclass instances share issue 1 properties of the parent class reference
- When subclassed instance, you can pass parameters to parent
- Can implement multiple inheritance (call more than one parent class object)
Disadvantages:
- Examples of instances is not the parent class, the only instance of a subclass
- Examples can inherit attributes and methods of the parent class, the prototype can not inherit properties / methods
- Multiplexing function can not be achieved, a copy of each sub-class has a parent class instance functions affect performance
(C) class inheritance
In ES6, we can use class
to achieve inheritance, and is simple to implement
Core: Use extends
show inherited from which parent, and must be called in the subclass constructor super
, this code can be seen as Animal.call(this, name)
c
lass
的本质就是函数
class DaZhong extends Car { constructor(name){ super(name); this.name= ame || '迈腾'; } } maiTeng the let = new new dazhong (); console.log (maiTeng.name) // Magotan console.log (maiTeng.addOil (30)) // Magotan day refueling 30L console.log (maiTeng.driver ()) console.log(maiTeng instanceof Car); //true console.log(maiTeng instanceof DaZhong) // true