I have this function:
function Entity(textureSrc)
{
var entity = {
texture: textureSrc,
position: { x: 0, y: 0 },
test: this.texture,
construct: function()
{
alert(this.test);
}
}
return entity;
}
And then this test code:
var testObject = Entity("Textures/AirTexture.png");
testObject.construct();
As a test, I am trying to utilise the value of entity.texture when creating a new property for entity – I can’t quite figure out what the syntax to do this would be.
I’ve tried:
test: this.texturetest: entity.texturetest: texture
But none of these work; they all result in undefined.
Also – is the use of the word this within the construct method correct for accessing test or should this be done differently?
On the “test” line, “this” doesn’t exist yet (since you’re in the middle of defining it).
It is, however, valid to use this in the construct function because this will exist when that function is evaluated (and will point to what you expect it to unless you rebind the function).