js 两种常用的类型

111 阅读1分钟

1.基于对象的继承

        //  Object.create是基于对象的继承最简单的方式
       var person = {
           name:'曹操',
           sayName(){
               console.log(this.name)
           }
       };

        var myPerson = Object.create(person); // 创建一个新对象myPerson ,它集成自person。
    //    var myPerson = Object.create(person,{
    //        name:{
    //            value:'李白',
    //        }
    //    }); // 创建一个新对象myPerson ,它集成自person。

       myPerson.sayName(); // 曹操
       person.sayName(); // 曹操     

2.基于类型的继承

// 基于类型的继承一般需要两不, 首先,原型继承;然后,构造器继承。
  function Person(name){
      this.name;
  }   

  function Author(name){

      Person.call(this,name); // 继承构造器
      console.log(this);
  }

  Author.prototype = new Person(); // 原型继承