Is there another way in terms of syntax to access "age"? this.age wont work
I think you're having trouble with scope, and anonymous functions.
var myObject = {
giveMeSomeName: function() {
console.log(this)
this.age = "20"
}
}
here this
actually references the function giveMeSomeName
, not myObject
. if you want to associate a value with myobject
, declare it in the scope of that object, OR (common practice) pass the scope of your myObject
to the function giveMeSomeName
:
var myObject = {
var that = this;
giveMeSomeName: function() {
console.log(that)
that.age = "20"
}
}
this
, is a reference to the current function, obj, or window.
enemy.position.x += 3; //how can I access 'enemy' from above?
you need to declare enemy as a global object (window.enemy) or move the functions inside the scope of enemyMovement
:
function enemyMovement(enemy) {
enemy.position.x += 3;
enemyFunctionA();
enemyFunctionB();
}