//ES6的类及继承
{
class Test {
constructor(obj) {
this.name = obj.name;
}
action() {
console.log('打豆豆!')
}
}
class Test1 extends Test {
constructor(obj) {
super(obj);
this.age = obj.age;
}
}
let instance = new Test1({ name: '小米', age: '18' });
console.log(instance);
}
//ES5的类和继承(构造函数模式))
//实例的constructor指向当前对象的构造函数
es5的继承
{
function Test(obj) {
this.name ? this.name = obj.name : this.name = null;
this.action = function() {
console.log('打豆豆!');
}
}
function Test1(obj) {
// Test.apply(this, arguments)
this.age = obj.age;
}
//原型链继承(父类的实例作为子类原型)
//优点:
//简单
//实例是父类的实例也是子类的实例
//缺点:
//无法向父级传参.
//实例丢失constructor
//无法实现多继承
Test1.prototype = new Test();
Test1.prototype.constructor = Test;
var instance = new Test1({ name: '小米1', age: '18' });
console.log(instance)
}
评论 (0)