0

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?
 };
}
asked Jul 28, 2012 at 5:01
1
  • read this Commented Jul 28, 2012 at 5:05

1 Answer 1

7

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.