js中的new

2018-06-12  本文已影响9人  阿亮2019
function Human(options){
  this.name = options.name;
  this.city = options.city;
  
  this.__proto__ = Human.prototype;

} // 构造函数结束

Human.prototype.species = 'people';
Human.prototype.walk = function () {
  console.log(this.name + ' walking in ' + this.city);
}
Human.prototype.useTools = function () {
  console.log('useTools');
}

var human = new Human({name:'Frank', city: 'Hangzhou'})
var human2 = new Human({name:'Jack', city: 'Hangzhou'})


console.log(human.species + ': ' + human.name);
console.log(human.species + ': ' + human2.city);
human.walk();
console.log(human.__proto__.constructor === Human);

output:

people: Frank
people: Hangzhou
Frank walking in Hangzhou
true
上一篇 下一篇

猜你喜欢

热点阅读