如何检查函数是否已定义? [英] How to check if a function is already defined?

查看:356
本文介绍了如何检查函数是否已定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to check if a function is already defined ?

推荐答案

检查函数是否存在的Javascript函数。

使用 jQuery.isFunction ()你可以测试一个参数来检查它是否是(a)定义的,(b)是函数类型。既然你要求使用jQuery,这个函数就会让你感到满意。

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(YourFunction)

如果您因任何原因不希望使用jQuery,这里有一个基于 Idealog 将检查变量是否为函数

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.

function isFunction(fn){
    return typeof fn === 'function'
}

有时你已经知道它是一个函数,为了优化,没有理由重新检查它的类型,在这种情况下,这里只是检查<$ c $的函数c>变量 [可能是函数]已定义

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 typeof(foo) !== 'undefined'
}

如何使用这些函数

使用jQuery:

function foo(){}
if(jQuery.isFunction(foo)) alert('This is a function');

使用上面提供的任何非jQuery Javascript函数。根据使用环境,这些功能可能是可靠的,也可能是不可靠的。查看以下内容

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

function foo(){}
if(isFunction(foo)) alert('is of type function');
if(isDefined(foo)) alert('if this is a function, it is defined');

检查未定义和使用 jQuery.isFunction

if (typeof myfunc !== 'undefined' && $.isFunction(myfunc)) {
    //do something
}

来源

是jQuery.isFunction()上位?

根据 Kyle Florence jQuery.isFunction()在某些情况下它可能更胜一筹。在使用jQuery方法的某些边缘情况下特别有用,请参阅他的解释

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.


在某些
浏览器的某些情况下,事情是错误的
作为函数类型返回,或
事实上功能的东西是
作为另一种类型返回。您可以在此处看到
的几个测试用例:
https://github.com/ jquery / jque ...

一个例子:

var obj =
document.createElement(object);

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

// Firefox说这是一个函数
typeof obj; // =>function

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

请记住,这些主要是边缘
的情况,但$ .isFunction为
的原因只是为了积极关于
这是一个函数(对于jQuery
库本身来说,
非常重要,对于你的代码可能没有多少b $ b。)

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

感谢 patrick dw 指出Kyles文章。 (Patrick DW删除了他的帐户)

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

来自 jQuery.com


注意:从jQuery 1.3开始,浏览器提供的函数如alert()
和getAttribute()之类的DOM元素方法不能保证在Internet Explorer等浏览器中被检测为

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.

这篇关于如何检查函数是否已定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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