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?
};
}
You need to use
thisto 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 itsthiscan be bound to any value.