1

I have an object with callback-method which I should extend. The object is elementHandle and method onCheckURLDone(result, data) is called internally somewhere. I need to execute some extended code and call back original function code. I tried something like this:

  var cachedCallback = elementHandle.onCheckURLDone;

  elementHandle.onCheckURLDone = (function(result, data){
    console.log("Some extended code...");

    return function(result, data) {
        return cachedCallback.apply(elementHandle, arguments);
    }
  });

But original code is not called here. What's wrong?

1
  • Try removing the return function()... line and the close brace corresponding to it? Commented Feb 20, 2016 at 11:57

1 Answer 1

3

Try this:

var cachedCallback = elementHandle.onCheckURLDone;
elementHandle.onCheckURLDone = function (result, data) {
    console.log('Some extended code...');
    return cachedCallback.apply(elementHandle, arguments);
};

Original callback in your code is called inside returned function. So if you want it to be executed, you have to call it like this:

elementHandle.onCheckURLDone(someResult, someDate)(someResult, someDate);

I suppose it is not what you want.

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

2 Comments

"try this" answers are not usually useful, there's almost always something useful to say rather than just dumping out code.
It was easy, don't know why I wrote that return wrapper :) Thank you

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.