First things first, Game.help
will attach a function to the function Game
, not to instances of the Game
object.
That is, Game.help = function () { ... }
will allow Game.help()
but not new Game().help()
. This is the equivalent of a static method in most OO languages.
What you can do, but isn't very idiomatic, is to change help.js
to:
Game.prototype.help = function () {
...
}
This will attach the function as a method, so any instance of Game
can have help()
called on it.
Extending a class' prototype from another module(/file) is kind of sketchy, though, since it adds an implicit dependency (implicit preventing the browser from enforcing it, often leading to errors later when you forget about the dependency and change something that looks unrelated).
Until the ES7 extension methods proposal is finalized (and ES7) lands, you may want to consider using helper methods that take their scope as the first parameter:
help(game, ...) {
alert('help for ' + game.name);
}
This is still less than ideal, but somewhat safer.