Cannot call prototype method from another function

亡梦爱人 提交于 2019-12-24 13:40:51

问题


{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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!