You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Object.create = function(obj) {
function Temporary(){};
Temporary.prototype = obj;
return new Temporary();
}
ES6继承
ES6类的数据类型就是函数,类本身就指向构造函数。typeof Father 返回function
class Father {
constructor(name) {
this.name = name;
}
}
class Child extends Father {
constructor(name, age) {
super(name);
this.age = age;
}
}
let instance1 = new Child("lxw", 26);
第一种挺常用的(构造函数默认首字母大写,约定俗成):
第二种继承方式:
Object.create实现原理:
ES6继承
ES6类的数据类型就是函数,类本身就指向构造函数。typeof Father 返回function
子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。子类只有调用super之后,才可以使用this关键字,否则会报错。这是因为子类实例的构建,是基于对父类实例加工,只有super方法才能返回父类实例。
The text was updated successfully, but these errors were encountered: