Hi, Is there anyway to stop function c executes again after it is being executed by function a. var a = function() { c(1); } var b = function() { c(2); } var c = function(i) { var d = 3 + i; console.log(d); } a(); b(); Code (markup): Thank you,
Javascript supports function overloading; more so since you made it a classed variable. So undefine it in 'a'. c = null;
Well I'm not necessarily sure it's a great idea to completely overwrite c. He merely wants to prevent it from executing in b is what I'm gathering. A basic example using the existing code would be to set a Boolean variable specifying whether or not c can be executed. var executeC = true; var a = function() { c(1); executeC = false; } var b = function() { if (executeC) { c(2); } } var c = function(i) { var d = 3 + i; console.log(d); } a(); b(); Code (markup):
Instead of setting it to null, I would set it to a null function. That way it wouldn't error if it is called again: var a = function() { c(1); c = function () {}; } Code (markup):
Your best bet is to do it this way :- var a = function (){ c(1,true); } var b = function (){ c(2,false);//or simply c(2); } var c = function (i,execute){ if(!execute)return; var d = 3 + i; console.log(d); } a(); b(); Code (markup):
Spaceman12 your code is wrong. In your example if you only call b(); without a(); beforehand the code in C will never execute even though it is intended to do so. I like Iconiplex implementation the best.