jquery javascript避免在条件下运行函数
我在js中有一组jquery和javascript函数。现在我想避免在某些条件下使用某些函数。例如:jquery javascript避免在条件下运行函数,javascript,jquery,preventdefault,Javascript,Jquery,Preventdefault,我在js中有一组jquery和javascript函数。现在我想避免在某些条件下使用某些函数。例如: function test () { alert("hi"); } function avoid () { if(condition) { //here avoid to call test } } 谢谢如果您想删除一个没有错误的函数(前提是函数的调用者不希望返回结果),您可以这样做 function avoid () { if(condition)
function test () {
alert("hi");
}
function avoid () {
if(condition) {
//here avoid to call test
}
}
谢谢如果您想删除一个没有错误的函数(前提是函数的调用者不希望返回结果),您可以这样做
function avoid () {
if(condition) {
test = function(){}; // does nothing
}
}
请注意,如果
avoid
函数位于不同的范围内,则可能需要进行调整。例如,如果在全局范围中定义了test
,那么您需要执行window.test=function(){}代码>我不确定是否理解了您的问题,但对测试函数的调用是否在如下所示的避免函数中:
function test () {
alert("hi");
}
function avoid () {
if(condition) {
//here avoid to call test
return false;
}
test();
}
那么一个简单的return false可以解决您的问题,我不确定您的函数是否使用了事件,但如果是这样的话,这是一篇好文章,您可以阅读关于preventDefault和return false的内容:
如果试验处于相同的避免水平,则如下所示:
function test () {
alert("hi");
}
function avoid () {
if(condition) {
//here avoid to call test
return false;
}
return true;
}
// below is your functions call
check = avoid();
if(check){
test();
}
这篇文章解释了如何退出函数:
我希望这会有所帮助。您能分享更多信息,以了解您的需求吗?一个JSFIDLE链接就好了。。我建议不要调用该函数。是否可以在test()
本身中计算condition
?例如functiontest(){if(!condition){alert(“hi”);}}
,您可以添加test.old=test
,以保留旧函数