如何检查函数是否已定义?

时间:2022-11-05 08:40:34

How to check if a function is already defined ?

如何检查函数是否已定义?

10 个解决方案

#1


39  

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)类型为“function”。既然你问了jQuery,这个函数就会让你觉得痒痒的。

jQuery.isFunction(YourFunction)

If you wish not to use jQuery for whatever reason, here's a barebones function based on code from Idealog that will check if the variable is of type function.

如果您不希望出于任何原因使用jQuery,这里有一个基于Idealog代码的基本函数,它将检查变量是否为类型函数。

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

有时,您已经知道它是一个函数,为了优化,找不到任何理由重新检查它的类型,在本例中,这里的函数只是检查变量(可能是一个函数)是否被定义。

function isDefined(foo){
    return 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-jQuery Javascript functions provided above. Depending on the context of usage, these functions may, or may not be reliable. See more below

使用上面提供的非jquery Javascript函数。根据使用环境的不同,这些函数可能可靠,也可能不可靠。请参见下面的更多

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
}

Source

Is jQuery.isFunction() superior?

jQuery.isFunction()优越吗?

According to Kyle Florence jQuery.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...

在某些浏览器中,某些东西作为“函数”类型不正确地返回,或者实际上是函数的东西作为另一种类型返回。这里有几个测试用例:https://github.com/jquery/jque.。

One example:

一个例子:

var obj = document.createElement("object");

var obj = document.createElement(“对象”);

// Firefox says this is a function typeof obj; // => "function"

// Firefox说这是obj的函数类型;/ / = >“功能”

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).

记住,这些大多是边缘情况,但原因是$。isFunction是简单地对某个函数(对于jQuery库本身很重要,可能对您的代码不那么重要)做肯定。

Thanks patrick dw for pointing out Kyles Article. (Patrick DW deleted his account)

谢谢patrick dw指出Kyles的文章。(Patrick DW删除了自己的账号)

From jQuery.com

从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)中检测到。

#2


16  

Like so:

像这样:

if (typeof myFunc != 'undefined') {
    // Assign myFunc
}

Don't just test it against undefined, which is not a constant and can be reassigned.

不要只对未定义的函数进行测试,它不是常量,可以重新赋值。

#3


3  

if (typeof(functionName) == 'function') {
}

.

#4


2  

if (myFunc !== undefined) {
  // myFunc is defined
  foo();
}

or

if (myFunc === undefined) {
  // myFunc is not defined
  qux();
}

#5


2  

typeof return string,so you can use the JavaScript typeof operator to find the type of a JavaScript variable.

返回字符串类型,因此可以使用JavaScript类型运算符查找JavaScript变量的类型。

if ( typeof(youerFunctionName) === 'undefined' )
{
    console.log('undefined');
}

#6


1  

if ([function] != undefined) {
  [do stuff]
}

You can also use jQuery's isFunction() to check it.

您还可以使用jQuery的isFunction()检查它。

#7


1  

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
}

#8


1  

try this one

试试这个

/*********************************/

if(typeof(callback) == 'function')

/*********************************/

here is a working example

这里有一个工作示例。

#9


0  

function test(){}

if(typeof test  != "undefined")
    // function is allready defined

#10


0  

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');
}

#1


39  

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)类型为“function”。既然你问了jQuery,这个函数就会让你觉得痒痒的。

jQuery.isFunction(YourFunction)

If you wish not to use jQuery for whatever reason, here's a barebones function based on code from Idealog that will check if the variable is of type function.

如果您不希望出于任何原因使用jQuery,这里有一个基于Idealog代码的基本函数,它将检查变量是否为类型函数。

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

有时,您已经知道它是一个函数,为了优化,找不到任何理由重新检查它的类型,在本例中,这里的函数只是检查变量(可能是一个函数)是否被定义。

function isDefined(foo){
    return 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-jQuery Javascript functions provided above. Depending on the context of usage, these functions may, or may not be reliable. See more below

使用上面提供的非jquery Javascript函数。根据使用环境的不同,这些函数可能可靠,也可能不可靠。请参见下面的更多

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
}

Source

Is jQuery.isFunction() superior?

jQuery.isFunction()优越吗?

According to Kyle Florence jQuery.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...

在某些浏览器中,某些东西作为“函数”类型不正确地返回,或者实际上是函数的东西作为另一种类型返回。这里有几个测试用例:https://github.com/jquery/jque.。

One example:

一个例子:

var obj = document.createElement("object");

var obj = document.createElement(“对象”);

// Firefox says this is a function typeof obj; // => "function"

// Firefox说这是obj的函数类型;/ / = >“功能”

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).

记住,这些大多是边缘情况,但原因是$。isFunction是简单地对某个函数(对于jQuery库本身很重要,可能对您的代码不那么重要)做肯定。

Thanks patrick dw for pointing out Kyles Article. (Patrick DW deleted his account)

谢谢patrick dw指出Kyles的文章。(Patrick DW删除了自己的账号)

From jQuery.com

从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)中检测到。

#2


16  

Like so:

像这样:

if (typeof myFunc != 'undefined') {
    // Assign myFunc
}

Don't just test it against undefined, which is not a constant and can be reassigned.

不要只对未定义的函数进行测试,它不是常量,可以重新赋值。

#3


3  

if (typeof(functionName) == 'function') {
}

.

#4


2  

if (myFunc !== undefined) {
  // myFunc is defined
  foo();
}

or

if (myFunc === undefined) {
  // myFunc is not defined
  qux();
}

#5


2  

typeof return string,so you can use the JavaScript typeof operator to find the type of a JavaScript variable.

返回字符串类型,因此可以使用JavaScript类型运算符查找JavaScript变量的类型。

if ( typeof(youerFunctionName) === 'undefined' )
{
    console.log('undefined');
}

#6


1  

if ([function] != undefined) {
  [do stuff]
}

You can also use jQuery's isFunction() to check it.

您还可以使用jQuery的isFunction()检查它。

#7


1  

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
}

#8


1  

try this one

试试这个

/*********************************/

if(typeof(callback) == 'function')

/*********************************/

here is a working example

这里有一个工作示例。

#9


0  

function test(){}

if(typeof test  != "undefined")
    // function is allready defined

#10


0  

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');
}