0

What is the difference between this:

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

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

And this:

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

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

Are there any significative gains in using prototype?

MurifoX
  • 14,991
  • 3
  • 36
  • 60

1 Answers1

0

Methods defined with Hero.prototype.myMethod = function() {} will also be be accessible by objects that are children of Hero.

m_vdbeek
  • 3,704
  • 7
  • 46
  • 77