js继承重要的2种方式

(1)原型链继承

function Parent() {  //   父类

  this.name = 'maxixi'

function Son() { //  子类

}

Son.prototype = new Parent()  // 父类上的属性和方法赋值给子类的原型上

var son = new Son()   //  实例化子类

console.log(son.name)  // maxixi

console.log(son instanceof Son) // true  son的原型链上有Son原型 ,所以 son 是 Son的实例

 

(2)构造继承

function Parent() {

  this.name = 'maxixi'

}

function Son () {

  Parent.call(this) //将父类的属性和值都复制给子类

  this.name = 'Tom'

}

var son = new Son()

console.log(son.name) // Tom 子类改写了父类的值

posted @ 2017-03-30 11:47  马习习  阅读(243)  评论(0编辑  收藏  举报