Javascript:给出的函数为空? [英] Javascript : is given function empty?

查看:100
本文介绍了Javascript:给出的函数为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们有一个函数调用

function doSomethingAndInvokeCallback(callback){
    // do something
    callback();
}

我可以检查给定的参数是否为函数 if( typeof callback =='function')

I can check if given argument is function if(typeof callback == 'function')

如果给定的回调函数是函数且不为空,我如何发现?

How can I discover, if given callback function is function and isn't empty?

喜欢

doSomethingAndInvokeCallback(function(){
    //nothing here
})


推荐答案

没有完全知道函数是否为空的可靠方法,因为JS中有多种函数,一些用JS实现,一些用本机代码实现,你无法确定传入的函数是否有任何作用。如果要将传入函数限制为仅非常简单的JS函数,可以使用其他答案中概述的机制(检查函数的来源)。但是,除了严格控制的情况之外,我不建议这样做,因为有很多合法的javascript方法可以打破这种情况。

There is no totally reliable way to know if a function is empty because there are multiple kinds of functions in JS, some implemented with JS and some implemented with native code and you can't know for sure whether the function passed in does anything or not. If you want to limit the passed in function to only very simple JS functions, you could use the mechanisms outlined by other answers here (examining the source of the function). But, I would not recommend doing that in anything but a tightly controlled situation because there are lots of legal javascript ways to break that.

我建议您应该更改你的函数参数的契约,并让调用者传递null或不传递任何东西(这将使参数 undefined )而不是一个空函数。然后,很清楚他们是否打算有一个被调用的函数。如果它们然后传递一个空函数而不是null或undefined,它们将获得函数接口指定的行为。调用者可以选择所需的行为,并且您可以以更安全的方式实现您的功能。

I would suggest that you should change the contract of your function arguments and have the caller pass null or not pass anything (which will make the argument undefined) rather than an empty function. Then, it will be very clear whether they intend to have a function called or not. If they then pass an empty function instead of null or undefined, they are getting the behavior that the interface of the function specifies. The caller can choose the desired behavior and you can implement your function in a more failsafe manner.

此外,您问题中的一个主要假设不太正确。您无法安全地使用 typeof x ==function来确定某些函数是否在某些类型的函数中在某些旧版本的IE中无法可靠地运行。如果你想学习如何检测某些东西是否是一个函数,你可以在这里学习jQuery(即使你没有使用它)。 jQuery有一个内部一直使用的函数,叫做 jQuery.isFunction(),返回一个bool。它主要用于测试参数以查看函数是否被传递。

Also, one of your main suppositions in your question is not quite right. You cannot safely use typeof x == "function" to determine if something is a function as that will not work reliably in some older versions of IE for some types of functions. If you want to learn how to detect if something is a function at all, you can learn from jQuery here (even if you're not using it). jQuery has a function it uses internally all the time called jQuery.isFunction() that returns a bool. It uses that mostly for testing arguments to see if a function was passed.

在内部,它调用:

Object.prototype.toString.call(o)

然后检查结果。如果结果中包含Function,则测试参数是一个函数。

and then examines the result. If the result has "Function" in it, then test test parameter is a function.

因此,使用jQuery中使用的相同技术,您可以构建自己的简单小 isFunction 例程如下:

So, using the same technique used in jQuery, you could build your own simple little isFunction routine like this:

function isFunction(test) {
    return(Object.prototype.toString.call(test).indexOf("Function") > -1);
}

当然,如果你有jQuery可用,你可以使用它自己的版本:

Of course, if you have jQuery available, you could just use it's own version:

jQuery.isFunction(o)

如果对潜在的跨浏览器兼容性问题存在疑问,我会发现它有助于了解其中一个大型库如何解决问题,即使您不打算使用该库。您可以确定这些库已针对许多浏览器进行了审查,因此他们使用的技术是安全的。你有时必须解开他们可能用来弄清楚他们真正在做什么的所有内部例程(这个函数的情况就是如此),但是你可以节省很多腿部工作。

When there are questions with potential cross browser compatibility issues, I find it instructional to look at how one of the big libraries solves the issue, even if you aren't going to be using that library. You can be sure that the libraries have been vetted against many browsers so a technique they are using is safe. You sometimes have to unwrap all their own internal routines they may use to figure out what they're really doing (which was the case for this function), but you can save yourself a lot of legwork.

你可以在这里看到一个工作试验台: http://jsfiddle.net/jfriend00/PKcsM/

You can see a working test bed for this here: http://jsfiddle.net/jfriend00/PKcsM/

在现代浏览器中 typeof fn ===function,但在旧版IE中,一些函数给出一个 typeof ===object这可能就是为什么jQuery使用这个在旧版IE中工作的其他方法。

In modern browsers typeof fn === "function", but in older versions of IE, some functions give a typeof === "object" which is probably why jQuery uses this other method which does work in those older versions of IE.

这篇关于Javascript:给出的函数为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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