0

这有什么区别:

function Hero(options) {
  options = options || {}
  this.nickname = options.nickname || 'Default 1';
  this.hp = options.hp || '50'
}

Hero.prototype.walk = function() {
  console.log('Hero walked');
}

和这个:

function Hero(options) {
  options = options || {}
  this.nickname = options.nickname || 'Default 1';
  this.hp = options.hp || '50'

  this.walk = function() {
    console.log('Hero walked')
  }
}

使用有什么明显的收获prototype吗?

4

1 回答 1

0

用 定义的方法Hero.prototype.myMethod = function() {}也可以被 的子对象访问Hero

于 2013-08-12T14:24:07.323 回答