2

So I've been trying to get this callback working inside of an object and I was just wondering if:

1) it's possible, 2) special syntax is required.

So instead of just having a regularly declared function:

function callback() {}

And passing that to another function by reference:

doSomething(callback)

Im trying to pass an object property that contains a function:

   {
     prop: function() {}
   } 

and send that as a call back.

The problem is, passing the reference of prop throws an error:

doSomething(prop): 

throws: Uncaught SyntaxError: Unexpected token :

If a complete example is needed, let me know.

Thanks, Ken

** Edit **

Okay so after some confusion I'm typing a fairly complete example.

var obj = {
    init: function() {
        this.some_function(this.callback_function)
    },
    some_function: function(callback) {
      callback();
    },
    callback_function: function() {
      // Callback!
    }
}

This is throwing the unexpected token error unless I put the () when I pass this.callback_function as an argument to some_function().

Thanks and sorry for not posting a fuller-example earlier.

Ken

1
  • "This is throwing the unexpected token error": No, it doesn't. Just keep in mind that the this keyword does not refer to obj when obj.callback_function is called. Commented Aug 28, 2012 at 9:03

5 Answers 5

3

I realize that this is not the solution to the OP's question but I google for "passing object methods as call backs" and this came up. I also found a link http://bitstructures.com/2007/11/javascript-method-callbacks.html which helped me understand how to do it in js. HTH

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response :) Though I solved this long ago, it's nice to know the community is a very helpful one.
1

Just pass the method:

var obj = {
    prop: function() {}
};
doSomething(obj.prop);

3 Comments

Sorry, I'll post a full example in a bit - but this is what I am currently doing. Throws unexpected token error unless you include the () with the argument.
@Ken I'm still waiting for your example. I'm not sure I got you question, but I'm afraid that your actual problem is Javascript syntax.
sorry for taking so long, I'm on a family vacation and was shuttled out the door immediately after posting this.
1

When you do obj.func as an argument, it passes the function losing this from the scope.

some_function: function(callback) {
    callback.call(this);
},

Comments

0

you are mistyping the semicolon at the end of doSomething(prop):

1 Comment

Sorry - I typed that from my phone. Typing a full example now.
0

Sorry for the late response ( I was on vacation ), It was a syntax error, I forgot a comma delimiter in my object method.

Comments

Your Answer

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.