• Advertisement
Sign in to follow this  

[JS] Confused how to retrieve variable value from within a function

This topic is 1902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having problems seeing how to solve the particular issue of returning the tile size to the caller. I have my own namespace called "goon" where i've added different functions, for example Tile below which i use to build my level. If i try:

console.log(goon.Tile.size); // This returns undefined.
console.log(goon.Tile.getTileSize()); // Throws the error: goon.Tile.getTileSize is not a function

[source lang="jscript"]goon.Tile = function(type, col, row) {
this.type = type;
this.size = 30;
this.top = row * this.size;
this.left = col * this.size;
this.bottom = row * this.size + this.size;
this.right = col * this.size + this.size;
this.getTileSize = function() {
return this.size;
};
}[/source]

So i tried adding a prototype and removed the above but this till doesn't work so obviously i'm doing something wrong here. I just need to get the size value from the function, is this even possible?

[source lang="jscript"]goon.Tile = function(type, col, row) {
this.type = type;
this.size = 30;
this.top = row * this.size;
this.left = col * this.size;
this.bottom = row * this.size + this.size;
this.right = col * this.size + this.size;
}

goon.Tile.prototype.getTileSize = function() {
return this.size;
};[/source]

Is the problem that i need to invoke or call the function first? Maybe you can't have a function within a function. Anyway, any help would be apprecieated.

Share this post


Link to post
Share on other sites
Advertisement
goon.Tile is a class, not an object.
"this" is applied on object, as far as I understand.
You need to write as,

function TileClass(blah) {
this.type = type;
// blah
}

goon.Tile = new TileClass(blah).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement