美文网首页
js 中几种指定一个对象的原型对象的方式

js 中几种指定一个对象的原型对象的方式

作者: 施主画个猿 | 来源:发表于2018-12-14 11:14 被阅读0次

    一 _proto_

    不推荐

    二 Object.create()

    Object.create() 方法创建一个新对象,使用现有的对象来提供新创建的对象的proto

    const person = {
      isHuman: false,
      printIntroduction: function () {
        console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
      }
    };
    
    const me = Object.create(person);
    
    me.name = "Matthew"; // "name" is a property set on "me", but not on "person"
    me.isHuman = true; // inherited properties can be overwritten
    
    me.printIntroduction();
    // expected output: "My name is Matthew. Am I human? true"
    

    三 Object.setPrototypeOf()

    Object.setPrototypeOf() 方法设置一个指定的对象的原型 ( 即, 内部[[Prototype]]属性)到另一个对象或 null

    相关文章

      网友评论

          本文标题:js 中几种指定一个对象的原型对象的方式

          本文链接:https://www.haomeiwen.com/subject/lgbbhqtx.html