-1

Suppose I have a Javascript object (the curly braces indicate it is so):

{
 a: function (something) {
 return something*2;
 },
 b: function () {
 var c = this.a(2); //Does not work. Why?
 return c;
 }
}

What is the workaround to this?

asked Jan 17, 2013 at 21:00
6
  • This should work. How are you calling b? Commented Jan 17, 2013 at 21:02
  • 4
    The value of this is entirely dependent on how you are calling b() and cannot be answered otherwise. Commented Jan 17, 2013 at 21:06
  • Mathletics is correct. I was calling a from another function within b, which affects "this". The trick is to save "this" before entering that function within b. Commented Jan 17, 2013 at 21:13
  • Voted to close as not a real question. Commented Jan 17, 2013 at 21:15
  • Mathletics, I asked this question after searching around SO for an hour. I'm sure it will help someone else out. Commented Jan 17, 2013 at 21:17

2 Answers 2

2

It not work because when you access the method b it context isn't the instance from object that you made, it will try to search in the binded context or in window object.

var x = {
 a: function (something) {
 return something*2;
 },
 b: function () {
 var c = x.a(2); //Does not work. Why?
 return c;
 }
}

This way you are using the x as context to access the method a.

Or you can use a new operator to create your object and the method as it prototype or direct method.

When you do it, the result will be:

var x = function() {
 return {
 a: function (something) {
 return something*2;
 },
 b: function () {
 var c = this.a(2); //Does not work. Why?
 return c;
 }
 }
}

It will make you lose the prototype from x, when you make a new instance, but your code will work.

Example:

var y = new x();
console.log(y.b());
answered Jan 17, 2013 at 21:04
Sign up to request clarification or add additional context in comments.

2 Comments

I thought x = was implied on the question. If not, how could the OP possibly be calling b?
yes, x is inside b method context. I did just put there for an example of use. This way you cant make it won't work using x.b.apply(window); for example.
1

It works for me, you were missing a coma after a declaration.

{
 a: function (something) {
 return something*2;
 }, // -> this one
 b: function () {
 var c = this.a(2);
 return c;
 }
}

If you call b the function should return a(2) which is 2*2=4

alert(p.b());

http://jsfiddle.net/PNbXj/

answered Jan 17, 2013 at 21:03

1 Comment

Just a syntax error :) (I typed it manually and was wondering what was wrong, good find)

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.