检查变量是否为函数类型

本文翻译自:Check if a variable is of function type

Suppose I have any variable, which is defined as follows: 假设我有任何变量,定义如下:

var a = function() {/* Statements */};

I want a function which checks if the type of the variable is function-like. 我想要一个函数来检查变量的类型是否像函数一样。 ie : 即:

function foo(v) {if (v is function type?) {/* do something */}};
foo(a);

How can I check if the variable a is of type Function in the way defined above? 如何以上面定义的方式检查变量a是否为Function类型?


#1楼

参考:https://stackoom.com/question/PAsA/检查变量是否为函数类型


#2楼

Try the instanceof operator: it seems that all functions inherit from the Function class: 尝试使用instanceof运算符:似乎所有函数都继承自Function类:

// Test data
var f1 = function () { alert("test"); }
var o1 = { Name: "Object_1" };
F_est = function () { };
var o2 = new F_est();

// Results
alert(f1 instanceof Function); // true
alert(o1 instanceof Function); // false
alert(o2 instanceof Function); // false

#3楼

I found that when testing native browser functions in IE8, using toString , instanceof , and typeof did not work. 我发现在IE8中测试本机浏览器函数时,使用toStringinstanceoftypeof不起作用。 Here is a method that works fine in IE8 (as far as I know): 这是一个在IE8中工作正常的方法(据我所知):

function isFn(f){
    return !!(f && f.call && f.apply);
}
//Returns true in IE7/8
isFn(document.getElementById);

Alternatively, you can check for native functions using: 或者,您可以使用以下方法检查本机功能:

"getElementById" in document

Though, I have read somewhere that this will not always work in IE7 and below. 虽然,我已经在某处读过,这在IE7及以下版本中并不总是有效。


#4楼

There are several ways so I will summarize them all 有几种方法可以总结它们

  1. Best way is: 最好的方法是:
    \n\nfunction foo(v) {if (v instanceof Function) {/* do something */} }; function foo(v){if(v instanceof Function){/ *做某事* /}};\n\n
    Most performant (no string comparison) and elegant solution - the instanceof operator has been supported in browsers for a very long time, so don't worry - it will work in IE 6. 最常见的(没有字符串比较)和优雅的解决方案 - 在浏览器中支持instanceof运算符很长一段时间,所以不要担心 - 它将在IE 6中运行。
  2. Next best way is: 下一个最好的方法是:
    \n\nfunction foo(v) {if (typeof v === "function") {/* do something */} }; function foo(v){if(typeof v ===“function”){/ *做某事* /}};\n\n
    disadvantage of typeof is that it is susceptible to silent failure, bad, so if you have a typo (eg "finction") - in this case the `if` will just return false and you won't know you have an error until later in your code typeof缺点是它容易出现静音失败,很糟糕,所以如果你有一个拼写错误(例如“finction”) - 在这种情况下,`if`只会返回false而你以后才会知道你有错误在你的代码中
  3. The next best way is: 下一个最好的方法是:
    \n\nfunction isFunction(functionToCheck) { function isFunction(functionToCheck){\n    var getType = {}; var getType = {};\n    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; return functionToCheck && getType.toString.call(functionToCheck)==='[object Function]';\n} }\n\n
    This has no advantage over solution #1 or #2 but is a lot less readable. 这没有解决方案#1或#2的优势,但可读性差得多。 An improved version of this is 这是一个改进版本
    \n\nfunction isFunction(x) { function isFunction(x){\n    return Object.prototype.toString.call(x) == '[object Function]'; return Object.prototype.toString.call(x)=='[object Function]';\n} }\n\n
    but still lot less semantic than solution #1 但仍然比解决方案#1少得多的语义

#5楼

you should use typeOf operator in js. 你应该在js中使用typeOf运算符

var a=function(){
    alert("fun a");
}
alert(typeof a);// alerts "function"

#6楼

jQuery (deprecated since version 3.3) Reference jQuery (自3.3版以来已弃用) 参考

$.isFunction(functionName);

AngularJS Reference AngularJS 参考

angular.isFunction(value);

Lodash Reference Lodash 参考

_.isFunction(value);

Underscore Reference 下划线 参考

_.isFunction(object); 

Node.js deprecated since v4.0.0 Reference 自v4.0.0 引用以来,不推荐使用Node.js

var util = require('util');
util.isFunction(object);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值