0

I have the following problem:

I have a function

workspace.func = function() {console.log(5);}

I attach it as an event handler:

$(workspace).bind("ping", workspace.func);

Then, I change the function definition:

var cF = workspace.func;
workspace.func = function() {
   ...
   cf.call(this);
}

but

$(workspace).trigger("ping")
>>5

How can I properly wrap the function at runtime, so that the handler points to the changed one as well?

1 Answer 1

2

You can do it like this:

workspace.func = function() {console.log(5);}
$(workspace).bind("ping", function() {workspace.func()});

var cF = workspace.func;
workspace.func = function() {
   ...
   cf.call(this);
}

After reassigning the value of workspace.func, the ping event handler will go to the new function because it gets the function pointer from the variable and then executes it so if you change which function that variable points to, it will pick up the new value - unlike your original version which had a reference to the actual function so changing the workspace.func variable didn't do anything.

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

1 Comment

+1 Alternatively, you can have workspace.func wrap a different mmber function call, and reassign that function instead of workspace.func.

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.