jQuery的JavaScript避免在条件下运行功能 [英] jquery javascript avoid running function on condition

查看:78
本文介绍了jQuery的JavaScript避免在条件下运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在js中有一组jquery和一般的javascript函数.现在,我想避免某些功能在某些情况下可用.例如:

I've a set of jquery and generally javascript function in a js. Now I want to avoid some of the functions to be available on some condition. For example:

function test () {
    alert("hi");
}

function avoid () {
   if(condition)  {
     //here avoid to call test
   }
}

谢谢

推荐答案

如果要删除没有错误的函数(假设函数的调用者不希望返回结果),则可以这样做

If you want to remove a function without errors (provided the caller of the function doesn't expect a returned result), you might do

function avoid () {
   if(condition)  {
     test = function(){}; // does nothing
   }
}

请注意,如果您的avoid函数位于不同的范围内,则可能需要进行调整.例如,如果test是在全局范围内定义的,那么您将执行window.test = function(){};

Note that if your avoid function is in a different scope, you might have to adapt. For example if test is defined in the global scope, then you'd do window.test = function(){};

这篇关于jQuery的JavaScript避免在条件下运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆