JS 继承

182 阅读3分钟

1.继承的第一种方法:原型链

实现方法: 把A原型的属性传递给B作为B的属性

Grand.prototype.lastName = 'ji';
function Grand(){
}
var grand = new Grand();
Father.prototype = grand;
function Father(){
this.name='hehe';
}
var father = new Father();
Son.prototype = father;
function Son(){
}
var son = new Son();

确定就是Grand的lastName,而且继承了Father的name;

注意:

不要忘记原型链中默认存在Object

子类添加方法或重写超类方法要放在替换原型语句之后

通过原型链实现继承后,不能使用对象字面量的方式创建方法和属性,因为会重写原型链

通过原型链实现继承后,超类的引用类型属性会被所有实例共享

2.继承的第二种方法:借用构造函数

实现方法:在需要继承构造函数内调用外部构造函数,即使用call()或者apply(),使得外部构造函数作用域发生改变

function SuperType(name,age){
  this.name = name;
  this.age = age;
}
function SubType() {
  SuperType.call(this,'i','21')//继承SuperType,并传递参数
  this.job = 'actor'
}
console.log(SubType.name)//i
console.log(SubType.age)//21

3.继承的第三种方法:组合继承

实现方法:使用原型链实现对原型属性和方法的继承,借用构造函数实现对实例属性的继承

function SuperType(name,age){
  this.name = name;
  this.age = age;
  this.f = [1,2,3,4]
}
SuperType.prototype.sayName = function() {
  console.log(this.name)
}
function SubType(name,age) {
  SuperType.call(this,name,age)//继承SuperType,并传递参数
  this.job = 'actor'
}
SubType.prototype=new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayHello=function() {
  console.log('hello')
}
var h = new SubType('hua', 18);
h.sayName()//hua
h.f.push(5)
console.log(h.f)//1,2,3,4,5
var n = new SubType();
console.log(n.f)//1,2,3,4

4. 继承的第四方法:原型式继承

以一个对象为基础,生成新对象,再对新对象进行修改 超类引用类型的属性依然是共享的

var person = {
  name:'lily',
  age:'21',
  friends:[1,2,3]
}
var people = Object.create(person);
people.friends.push(4);
var human = Object.create(person);
console.log(human.friends)//1,2,3,4

5、继承的第五方法:寄生式继承

创建一个仅用来实现继承过程的函数,并在函数内部扩展对象,再将对象返回 此时父类的引用类型属性依然是被所有实例共享

function anotherFunction(original) {
  var clone  = Object(original);
  clone.sayHi = function() {
    console.log('hi')
  }
  return clone;
}
var person = {
  name:'lili',
  age:'21',
  f: [1,2,3]
}
var people1 = anotherFunction(person);
people1.f.push(4)
console.log(people1.f);// 1,2,3,4
var people2 = anotherFunction(person);
console.log(people2.f);// 1,2,3,4

6、继承的第六种方法:寄生组合式继承

通过借用构造函数来继承属性,通过原型链混成来继承方法 减少了一次父类构造函数的执行,父类引用类型的属性不被共享

function SuperType(name,age){
  this.name = name;
  this.age = age;
  this.f = [1,2,3,4]
}
SuperType.prototype.sayName = function() {
  console.log(this.name)
}
function SubType(name,age) {
  SuperType.call(this,name,age)
  this.job = 'actor'
}
function inherit(superType,subType) {
  var property =  Object.create(superType.property);// 创建父类原型的一个副本,并没有调用父类构造函数
  property.constructor = subType;// 使父类原型副本的constructor属性指向子类
  subType.property = property;// 子类的原型指向父类原型副本
}
inherit(SuperType,SubType)
var instance = new SubType('haha', 18);
instance.sayName()//haha
instance.f.push(5);
console.log(instance.f);//1,2,3,4,5
var ins = new SubType();
console.log(ins.f);//1,2,3,4


完美模式:圣杯模式

function inherit(Targer, Origin){
function F(){};
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constuctor = Target;
Target.prototype.uber = Origin.prototype;
}
//son.__proto__ -->new F().__proto__ -->Father.prototype
var inherit = (function(){
var F = function(){};    //闭包,变成私有化变量,在函数外部无法调用
return function(Targer, Origin)
{
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constuctor = Target;
Target.prototype.uber = Origin.prototype;
}
}());