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
letCart=function(){this.color=['red'];};Cart.prototype.getColor=function(){returnthis.color;}letChild=function(){// 核心Cart.call(this);}letresult=newChild();result.color.push('black');//console.log(result.getColor()); // TypeError: result.getColor is not a functionconsole.log(result.color);// ["red", "black"]letresult2=newChild();result2.color.push('yellow');console.log(result2.color);// ["red", "yellow"]
letSuperClass=function(name){this.name=name;this.list=[1,2,3];};SuperClass.prototype.getName=function(){console.log(`name = ${this.name}; list = ${this.list}`);}letSubClass=function(name,age){SuperClass.call(this,name);// 第二次执行构造函数this.age=age;}SubClass.prototype=newSuperClass();// 第一次执行构造函数// 重写SubClass.prototype的constructor属性,指向自己的构造函数SubClassSubClass.prototype.constructor=SubClass;SubClass.prototype.getAge=function(){console.log(this.age);}letresult=newSubClass('小米',2);result.list.push(4);// name = 小米; list = 1,2,3,4result.getName();// 2result.getAge();letresult2=newSubClass('滴滴',3);// name = 滴滴; list = 1,2,3,5result2.getName();// 3result2.getAge();
4. 原型式继承
缺点:
原型链继承多个实例的引用类型属性指向相同,存在篡改的可能。
无法传递参数
/*var obj = {};obj.__proto__ = F.prototype;F.call(obj);以 new 操作符调用构造函数的时候,函数内部实际上发生以下变化:1、创建一个空对象,并且this变量引用该对象,同时还继承了该函数的原型。2、属性和方法被加入到 this 引用的对象中。3、新创建的对象由 this 所引用,并且最后隐式的返回 this.*/Object.create=Object.create||function(object){letF=function(){};F.prototype=object;returnnewF();}letSuperType={name: '小米',friend: ['小李','小刚'],getName: function(){console.log(this.name);}};letresult1=Object.create(SuperType);result1.name='小莉';result1.friend.push('小红');result1.getName();console.log(result1.friend);// ["小李", "小刚", "小红"]letresult2=Object.create(SuperType);result2.name='小赵';result2.friend.push('小钱');result2.getName();console.log(result2.friend);// ["小李", "小刚", "小红", "小钱"]
classA{}classB{}Object.setPrototypeOf=Object.setPrototypeOf||function(obj,proto){obj.__proto__=proto;returnobj;}// B的实例继承A的实例Object.setPrototypeOf(B.prototype,A.prototype);// B 继承 A 的静态属性Object.setPrototypeOf(B,A);constresult=newB();
babel 对 ES6 extends 的转义结果
function_inherits(subClass,superClass){if(typeofsuperClass!=="function"&&superClass!==null){thrownewTypeError("Super expression must either be null or a function");}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor: {value: subClass,writable: true,configurable: true}});if(superClass)_setPrototypeOf(subClass,superClass);}function_setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function_setPrototypeOf(o,p){o.__proto__=p;returno;};return_setPrototypeOf(o,p);}
The text was updated successfully, but these errors were encountered:
1. 原型链继承
核心: 将父类实例作为子类原型
优点: 方法复用
缺点:
2. 借用构造函数继承
核心: 借用父类的构造函数来增强子类实例,等于是复制父类的实例属性给子类。
优点:
缺点:
3. 组合继承法
核心:
通过调用父类构造函数,继承父类的属性并保留传参的优点;然后通过将父类实例作为子类原型,实现函数复用。
优点:
缺点:
注意:
组合继承这种方式,要记得修复
Child.prototype.constructor
指向4. 原型式继承
缺点:
5. 寄生式继承
缺点:
6. 寄生组合式继承
7. 混入方式继承多个对象
8. ES6 Class extends
ES6实现继承的原理
babel 对 ES6 extends 的转义结果
The text was updated successfully, but these errors were encountered: