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?
};
}
1 Answer 1
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();
answered Jul 28, 2012 at 5:03
Sign up to request clarification or add additional context in comments.
Comments
lang-js
this