JS基础——实现对象继承的六种方法

    // 不使用Object.create()

    // method1:
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }
    function Child(){
      this.type = 'Child'
    }

    Child.prototype = new Parent()
    let child1 = new Child()
    let child2 = new Child()
    child1.name = 'xiaoming'
    console.log(child1.name,child2.name)  // xiaoming,xiaohong
    child1.list.push(4)
    console.log(child1.list,child2.list)  // [1,2,3,4],[1,2,3,4]
    // 问题:子类实例对象的引用数据类型之间会互相引用


    // method2:
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }
    function Child(){
      Parent.call(this)
      this.type = 'Child'
    }

    let child1 = new Child()
    child1.sayHi() // 无法调用

    // method3:
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }
    function Child(){
      Parent.call(this)
      this.type = 'Child'
    }    
    Child.prototype = new Parent()
    Child.prototype.constructor = Child
    // 可以正常使用,但是Parent执行了两次,加大了性能的开销

    // 使用Object.create()
    // method4:
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }
    
    let child1 = Object.create(Parent)
    let child2 = Object.create(Parent)
    child1.list.push(4)
    child2.list.push(4)
    console.log(child1.list) //[1,2,3,4,4]
    // Object.create是浅拷贝,会导致互相引用

    // method5:
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }
    function clone(parent){
      let child = Object.create(parent)
      child.prototype.method = function(){
        //.....
      }
    }
    let child = clone(Parent)
    // 不足之处和上面一样,浅拷贝

    // method5:
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }
    function clone(parent){
      let child = Object.create(parent)
      child.prototype.method = function(){
        //.....
      }
    }
    let child = clone(Parent)
    // 不足之处和上面一样,浅拷贝

    
    // method6
    function Parent(){
      this.name = 'xiaohong'
      this.list = [1,2,3]
    }
    Parent.prototype.sayHi = function(x) {
      console.log('Hi')
    }

    function Child(){
      Parent.call(this)
      this.name = 'Child'
    }
    Child.prototype = Object.create(parent.prototype) //使用Object.create,避免实例创建
    Child.prototype.constructor = Child
    Child.prototype.sayName = function(){}
    // 最完美的方法
    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值