I have a function I can’t modify:
function addToMe() { doStuff(); }
Can I add to this function? Obviously this syntax is terribly wrong but it’s the general idea…
function addToMe() { addToMe() + doOtherStuff(); }
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could store a reference to the original function, and then override it, with a function that calls back the original one, and adds the functionality you desire:
You can do this because JavaScript functions are first-class objects.
Edit: If your function receives arguments, you should use apply to pass them to the original function:
You could also use an auto-executing function expression with an argument to store the reference of the original function, I think it is a little bit cleaner: