继承性问题

1.对象的继承

var 新对象=object.create(被继承的对象)

新对象.__proto__==被继承的对象

<script>
   var wjl={money:10000000000}
   var wsc= Object.create(wjl)
   console.log(wsc)
   console.log(wsc.__proto__)
  </script>

打印结果:

{  }

{money:10000000000}

结论:通过Object.create(wjl)方法可以继承对象的属性

2.借用函数的继承

<script>
function Person(name,age){
  this.name=name;
  this.age=age;
}
function Student(name,age,score){
  Person.call(this,name,age);
  this.score=score
}
var zs=new Student('zs',15,95)
console.log(zs)
</script>

打印结果:Student {name: "zs", age: 15, score: 95}

结论:通过call()方法调用函数Person,Student继承了Person的属性。

3.原型继承

<script>
    function Person() {}
    Person.prototype.say = function () {
      console.log('我是say方法')
    }

    function Student() {}
    Student.prototype = new Person
    var zs = new Student()
    zs.say()
  </script>

打印结果:我是say方法

结论:通过Student.prototype = new Person,将say添加到Student的原型上,从而Student的实例可以访问到say方法

 

转载于:https://www.cnblogs.com/zhaodz/p/11593922.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值