Reputation: 75
I'm relatively new to javascript OOP, and have what I believe is a relatively basic question but I haven't been able to find any help through searching the web. Am I missing something or am I simply going about this the wrong way?
Here's my example code:
function Square( setSize, setX, setY ){
var size = setSize;
var xPos = setX;
var yPos = setY;
this.getCenter = function(){
return {
x: xPos + size*0.5,
y: yPos + size*0.5
};
};
this.moveX = function( magnitude ){
var currentPosition //=how do I access getCenter() from here?
};
}
Upvotes: 0
Views: 73
Reputation: 224862
You need to use this
to refer to the current object; it's not implicit, like in other languages, mainly because the function is just an object like any other and its this
can be bound to any value.
var currentPosition = this.getCenter();
Upvotes: 7