If I want to release a function called Myfunc
What is best way to achieve such task?
_emptyFunc=function(){}
If I want to release a function called Myfunc What is best way to
Share
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.
_emptyFuncis callable while callingnullthrows aTypeError:So using a no-op function has the advantage that you can simply call it unconditionally instead of having to check if it’s not null/undefined and possibly even test if it’s something callable.
Using
nullorundefinedhas the advantage that your code can avoid doing stuff only necessary when a callback is passed.If you actually plan to allow the JS engine to release the memory used by whatever function was stored in the variable before, set it to
null. Assigning a different function will also remove the (possibly) last reference to the function and thus allow the GC to collect it but you’ll have a new function that obviously uses some memory, too.