问题
{EDIT} Actual code here: http://jsfiddle.net/r79xtbdm/
I have a constructor function as follows (pseudo-code):
function Tile(<pass in arguments here>)
{
// set some variables here based on arguments passed (this.width, this.height) etc..
}
Outside of this constructor function, I use prototype to provide a method for the function as follows:
Tile.prototype.draw = function() {
// do some stuff
}
I have another function as follows:
function createTileMap()
{
var firstTile = new Tile(<arguments>);
firstTile.draw();
}
The issue I am having is that the firstTile object is being created (I can access it's properties). However, firstTile.draw raises an undefined function error. I assume this is a matter of scope but can't get my head around it. Why can the createTileMap function not see the prototype method that has been added to the tile constructor?
Thanks! Have been banging my head against a wall for a few hours with this.
来源:https://stackoverflow.com/questions/28069026/cannot-call-prototype-method-from-another-function