您的位置:首页 > 新闻动态 > 技术文章
2019-02-25

[javascript]create创建新对象和prototype原型对象创建的区别

Object.create()方法创建一个新对象

const person = {

  isHuman: false,

  printIntroduction: function () {

    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);

  }

};

const me = Object.create(person);

//var 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"

 

prototype

function person(){};

person.prototype.text= {

  isHuman: false,

  printIntroduction: function () {

    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);

  }

};

const me = new person();

me.name = "Matthew"; // "name" is a property set on "me", but not on "person"

me.isHuman = true; // inherited properties can be overwritten

 

me.text.printIntroduction();

// expected output: "My name is undefined. Am I human? false"

 


下一篇:[webpack]各种loader的安装和使用
上一篇:[javascript]声明变量的三种方式和区别

© 2010-2020 Beasure本硕科技. All Right Reserved. 备案号:粤ICP备14074318号