JavaScript继承

JavaScript继承

基本上,ES6 的class可以看作只是一个语法糖,它的绝大部分功能,ES5 都可以做到,新的class写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。

借助构造函数实现继承

  • 缺点:无法继承原型链上的属性和方法
function Person () {
  this.name = 'person1'
}
Person.prototype.sex = '10'
Person.prototype.sayName = function () {
  alert(this.name)
}
function Man () {
  Person.call(this)
  this.age = '24'
}
var m1 = new Man()
console.log(m1) // Man {name: "person1", age: "24"}  (注意:实例自身不存在sex属性和 
sayName方法)
// m1.sayName() // 报错 (因为原型链上也不存在)

借助原型链实现继承

  • 缺点:子类共享父类的引用属性
function Father () {
 this.Farr = [1, 2, 3]
}
function Child () { }
Child.prototype = new Father()
var c1 = new Child()
var c2 = new Child()
console.log(c1.Farr) // [1, 2, 3]
console.log(c2.Farr) // [1, 2, 3]
c1.Farr.push(4)
console.log(c1.Farr) // [1, 2, 3, 4]
console.log(c2.Farr) // [1, 2, 3, 4]

组合继承

  • 优点:子类不会共享父类中的属性,缺点:Mother构造函数被执行两次,无法确定实例直接父类了
function Mother () {
  console.log('init Mother...')
  this.Marr = ['a', 'b', 'c']
}
function Daughter () {
  Mother.call(this) // 执行一次Mother构造函数
}
Daughter.prototype = new Mother() // 再执行一次Mother构造函数
var d1 = new Daughter()
var d2 = new Daughter()
console.log(d1.Marr) //  ["a", "b", "c"]
console.log(d2.Marr) //  ["a", "b", "c"]
d1.Marr.push('d')
console.log(d1.Marr) // ["a", "b", "c", "d"]
console.log(d2.Marr) //  ["a", "b", "c"]
console.log(d2.__proto__.constructor === Mother) // true(期望值是false,因为Daughter才是d2的直接父类)

组合继承优化(一)

  • 优点:防止调用两次构造函数,缺点:依旧无法确定实例直接父类
function Animal () {
  console.log('init Animal...')
}
function Cat () {
  Animal.call(this) // 执行一次Animal构造函数
}
Cat.prototype = Animal.prototype
var cat = new Cat()
console.log(cat.__proto__.constructor === Animal) // true(期望值是false,因为Cat才是cat的直接父类)

组合继承优化(二)

  • 解决无法确定实例直接父类问题
function God () {
  console.log('init Final...')
  this.name = 'god'
}
God.prototype.todo = function () {
  alert('todo')
}
function Son () {
  God.call(this)
}
// Son.prototype = God.prototype
Son.prototype = Object.create(God.prototype) // Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__
console.log(God.prototype)
console.log(Object.create(God.prototype))
Son.prototype.constructor = Son

var s = new Son // 无参数时,小括号可以省略
console.log(s)
console.log(s.__proto__.constructor === Son) // true (与期望值一直,完美!)
console.log(s.__proto__.constructor) // true (与期望值一直,完美!)

为什么要用Object.create()方法

  • Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__
  • 使用这个方法,传入null可以创建一个无任何属性的对象(原型上也没有)
  • 使用这个方法,会将现有对象放到__proto__属性中,会隐藏现有对象原型上的默认属性和方法,仅仅保留用户自己定义的。
// 普通方法创建对象的对象
console.log({})
// 使用Object.create创建的对象
console.log(Object.create(null))

// 使用Object.create,会将现有对象放到__proto__属性中
function God () {
  console.log('init Final...')
  this.name = 'god'
}
God.prototype.todo = function () {
  alert('todo')
}
function Son () {
  God.call(this)
}
// Son.prototype = God.prototype
Son.prototype = Object.create(God.prototype)
Son.prototype.constructor = Son

console.log(Son.prototype)
  • 普通方法创建对象的对象

    img

  • 使用Object.create创建的对象

    img

  • 使用Object.create()方法实现继承

    img

  • 不使用Object.create()方法实现继承

    img

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值