Javascript 如何检查函数是否已经定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5159652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a function is already defined?
提问by Benoit
How to check if a function is already defined ?
如何检查函数是否已经定义?
回答by Sandwich
Javascript functions that check if a function exists.
检查函数是否存在的 Javascript 函数。
With jQuery.isFunction()you may test a parameter to check if it is (a) defined and (b) is of type "function." Since you asked for jQuery, this function will tickle your fancy.
使用jQuery.isFunction()你可以测试一个参数来检查它是否是 (a) 定义的并且 (b) 是“函数”类型。既然您要求使用 jQuery,那么这个功能会让您大吃一惊。
jQuery.isFunction(YourFunction)
If you wish not to use jQuery for whatever reason, here's a barebones function based on code from Idealogthat will check if the variable is of type function
.
如果您出于任何原因不想使用 jQuery,这里有一个基于Idealog代码的准系统函数,它将检查变量是否为 类型function
。
function isFunction(fn){
return typeof fn === 'function'
}
Sometimes you already know it's a function and for the sake of optimization find no reason to recheck it's type, in this case, here's function that simply checks if the variable
[possibly a function] is defined
有时你已经知道它是一个函数,为了优化,没有理由重新检查它的类型,在这种情况下,这里的函数只是检查是否variable
定义了[可能是一个函数]
function isDefined(foo){
return typeof(foo) !== 'undefined'
}
How to use these functions
如何使用这些功能
Using jQuery:
使用jQuery:
function foo(){}
if(jQuery.isFunction(foo)) alert('This is a function');
With either of the non-jQueryJavascript functions provided above. Depending on the context of usage, these functions may, or may not be reliable. See more below
使用上面提供的任一非 jQueryJavascript 函数。根据使用的上下文,这些功能可能可靠,也可能不可靠。在下面查看更多
function foo(){}
if(isFunction(foo)) alert('is of type function');
if(isDefined(foo)) alert('if this is a function, it is defined');
Check both undefined and using jQuery.isFunction
检查未定义和使用 jQuery.isFunction
if (typeof myfunc !== 'undefined' && $.isFunction(myfunc)) {
//do something
}
Is jQuery.isFunction() superior?
jQuery.isFunction() 是否更胜一筹?
According to Kyle FlorencejQuery.isFunction()
it could superior in some situations. Particularly useful in some edge cases when using jQuery methods, see his explanation.
根据Kyle Florence 的说法,jQuery.isFunction()
它在某些情况下可能更胜一筹。在使用 jQuery 方法的某些边缘情况下特别有用,请参阅他的解释。
In certain situations in some browsers, things are incorrectly returned as the "function" type, or things that are in fact functions are returned as another type. There are several test cases you can see here: https://github.com/jquery/jque...
One example:
var obj = document.createElement("object");
// Firefox says this is a function typeof obj; // => "function"
Keep in mind these are mostly edge cases, but the reason $.isFunction was made was simply to be positive about something being a function (which can be quite important for the jQuery library itself, maybe not so much for your code).
在某些浏览器的某些情况下,事物被错误地作为“函数”类型返回,或者实际上是函数的事物作为另一种类型返回。您可以在这里看到几个测试用例:https: //github.com/jquery/jque...
一个例子:
var obj = document.createElement("object");
// Firefox 说这是一个函数 typeof obj; // => "函数"
请记住,这些大多是边缘情况,但创建 $.isFunction 的原因只是为了肯定某些东西是一个函数(这对于 jQuery 库本身来说可能非常重要,但对您的代码而言可能不是那么重要)。
Thanks patrick dwfor pointing out Kyles Article. (Patrick DW deleted his account)
感谢patrick dw指出凯尔斯文章。(Patrick DW 删除了他的账户)
From jQuery.com
Note: As of jQuery 1.3, functions provided by the browser like alert() and DOM element methods like getAttribute() are not guaranteed to be detected as functions in browsers such as Internet Explorer.
注意:从 jQuery 1.3 开始,浏览器提供的函数如 alert() 和 DOM 元素方法如 getAttribute() 不能保证在浏览器(如 Internet Explorer)中被检测为函数。
回答by Peter C
Like so:
像这样:
if (typeof myFunc != 'undefined') {
// Assign myFunc
}
Don'tjust test it against undefined
, which is not a constant and can be reassigned.
不要只是针对 测试它undefined
,它不是一个常数并且可以重新分配。
回答by Richard Dalton
if (typeof(functionName) == 'function') {
}
.
.
回答by Richard Dalton
if (myFunc !== undefined) {
// myFunc is defined
foo();
}
or
或者
if (myFunc === undefined) {
// myFunc is not defined
qux();
}
回答by George C
typeof return string,so you can use the JavaScript typeof operator to find the type of a JavaScript variable.
typeof 返回字符串,因此您可以使用 JavaScript typeof 运算符来查找 JavaScript 变量的类型。
if ( typeof(youerFunctionName) === 'undefined' )
{
console.log('undefined');
}
回答by Muhammad Tahir
回答by RDL
if ([function] != undefined) {
[do stuff]
}
You can also use jQuery's isFunction()to check it.
您也可以使用jQuery 的 isFunction()来检查它。
回答by Herms
Say your function is called func
:
假设你的函数被调用func
:
if (func) {
// Function is already defined (or at least something is defined there)
} else {
// Function is not defined
}
回答by jimmystormig
function test(){}
if(typeof test != "undefined")
// function is allready defined
回答by spacebiker
This is what I use to check if a function is already defined:
这是我用来检查函数是否已定义的方法:
if ( typeof(myFunc()) === 'undefined' )
{
console.log('myFunc undefined');
}
else
{
console.log('myFunc defined');
}