教程中论述继承
1、 旧方式
function animal(){}
animal.prototype = {
getname(){ }
}
function cat(){}
cat.prototype = new animal();
2、新方式
function animal(){}
animal.prototype = {
getname(){ }
}
function cat(){}
cat.prototype = Object.create(new animal());
但是只能继承一次,如果要是多继承呢?我的想法是使用Object.assign,但是assign无法执行深复制!如果使用getOwnPropertyDescriptors配合Object.assign可以实现多继承
function arm (){this.name="arm"}; arm.prototype={ getname(){}}
function leg (){this.name="leg"}; arm.prototype={setname(){}}
let base = {}
Object.assign(
//在原型获取其属性解构
{...Object.getOwnPropertyDescriptors(arm).prototype.value},
{...Object.getOwnPropertyDescriptors(leg).prototype.value},
//利用Object.getOwnPropertyDescriptors无法获取实例的__proto__的特性获取构造函数内this指向参数
{...Object.getOwnPropertyDescriptors(new arm())},
{...Object.getOwnPropertyDescriptors(new leg())}
)
//继承
function newFun(){}
newFun.prototype = base;
new newFun();//
这种多重的继承最大的问题是出现重复函数名会被覆盖,而且如果做好多重继承应该给出错误提示阻止其继承,