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方法

 

posted on 2019-09-26 19:42  宅到深夜  阅读(284)  评论(0编辑  收藏  举报