isFunction
标准写法是:
var isFunction = function(obj) {
return Object.prototype.toString.call(obj) === '[object Function]'
}
深层次:
[list]
有博客说, 之前的chrome会识别 new Regexp()为function, 没有测试之前的chrome, 现在的不会了
有博客说, document.write在IE下会识别为object, 其实新的写法, 依然还是object, 不信,jquery测试下, 同理getAttribute也是一样的
jquery的isFunction注释
// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
}
jquery中的 type(obj), 即是 Object.prototype.toString.call(obj)
jquery已经不支持DOM方法,以及像alert这样的方法, 因为他们在IE都返回false,无法区分
#2968 见 http://bugs.jquery.com/ticket/2968
[/list]
最简单的写法:
var isFunction = function(obj) {
return typeof obj === 'function'
}
这个写法, 在jquery的isFunction测试用例中, 全部通过 https://code.google.com/p/jqueryjs/source/browse/trunk/jquery/test/unit/core.js
简单的就是最好的~~
测试用例 (修改自jquery的isFunction测试用例)
<body>
<ul id="result"></ul>
<script>
var jQuery = {
isFunction: function(obj) {
return typeof obj === 'function'
}
}
function ok(bool, text) {
document.getElementById('result').innerHTML += ('<li>' + (bool ? '√':'×') + ' ' + text);
}
function test() {
// Make sure that false values return false
ok( !jQuery.isFunction(), "No Value" );
ok( !jQuery.isFunction( null ), "null Value" );
ok( !jQuery.isFunction( undefined ), "undefined Value" );
ok( !jQuery.isFunction( "" ), "Empty String Value" );
ok( !jQuery.isFunction( 0 ), "0 Value" );
// Check built-ins
// Safari uses "(Internal Function)"
ok( jQuery.isFunction(String), "String Function("+String+")" );
ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
// When stringified, this could be misinterpreted
var mystr = "function";
ok( !jQuery.isFunction(mystr), "Function String" );
// When stringified, this could be misinterpreted
var myarr = [ "function" ];
ok( !jQuery.isFunction(myarr), "Function Array" );
// When stringified, this could be misinterpreted
var myfunction = { "function": "test" };
ok( !jQuery.isFunction(myfunction), "Function Object" );
// Make sure normal functions still work
var fn = function(){};
ok( jQuery.isFunction(fn), "Normal Function" );
var obj = document.createElement("object");
// Firefox says this is a function
ok( !jQuery.isFunction(obj), "Object Element" );
// IE says this is an object
// Since 1.3, this isn't supported (#2968)
//ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
var nodes = document.body.childNodes;
// Safari says this is a function
ok( !jQuery.isFunction(nodes), "childNodes Property" );
var first = document.body.firstChild;
// Normal elements are reported ok everywhere
ok( !jQuery.isFunction(first), "A normal DOM Element" );
var input = document.createElement("input");
input.type = "text";
document.body.appendChild( input );
// IE says this is an object
// Since 1.3, this isn't supported (#2968)
//ok( jQuery.isFunction(input.focus), "A default function property" );
document.body.removeChild( input );
var a = document.createElement("a");
a.href = "some-function";
document.body.appendChild( a );
// This serializes with the word 'function' in it
ok( !jQuery.isFunction(a), "Anchor Element" );
document.body.removeChild( a );
// Recursive function calls have lengths and array-like properties
function callme(callback){
function fn(response){
callback(response);
}
ok( jQuery.isFunction(fn), "Recursive Function Call" );
fn({ some: "data" });
};
callme(function(){
callme(function(){});
});
}
test();
</script>
</body>
分享到:
相关推荐
修复 jQuery 中 isFunction 方法的 BUG
当然,jquery除了提供$.type的工具方法外,还提供了几个其他的工具方法:$.isFunction(), $.isArray(), $.isWindow(), $.isNumeric()等。这几个方法从方法名上就能看出其用途来,下面我们来一一讲解这几个方法在...
如果 FUN 是有效的 matlab 函数,则 TF = ISFUNCTION(FUN) 返回 1,否则返回 0。 Matlab 函数可以是字符串或函数句柄。 [TF, ID] = ISFUNCTION(FUN) 也返回一个标识符 ID。 ID 可以采用以下值: 1 : FUN 是一个函数...
Th is function is commonly referred to as dimming control. Th is article describes some basic LED theory and several techniques used to provide dimming control to switched-mode LED drivers.
describe what is function programming, why it matters.
function and pin compatible with the MAX9271. In high bandwidth mode, the parallel-clock maximum is 116MHz for 12-bit linear or combined HDR data types. The embedded control channel operates at 9.6...
function _isFunction(val) { if (!val) { return false; } return Object.prototype.toString.call(val) === '[object Function]'; } function _inArray(val, arr) { for (var i = 0, len = arr.length; i ; i...
} else if ( jQuery.isFunction( selector ) ) return jQuery( document ).ready( selector ); // Make sure that old selector state is passed along if ( selector.selector && selector.context ) { this....
js出现.endWith is not a function问题
The every part of function on AES, writen in java and in class.Two java txt in the rar, one is function and another is operator txt.
打开控制台发现报错:object is not a function。 感觉很奇怪,这块的功能最新没动过怎么会突然出问题了呢?上线时主流浏览器都测试过了呀。 虽然奇怪,但是还的解决问题。看着代码发现一个radio对象的name属性和一...
log ( 'this is function body.' ) ; return true ;} ;var ret = regex . exec ( fn . toString ( ) ) ;console . log ( ret [ 1 ] ) ; // => '\nconsole.log(\'this is function body.\');\nreturn true;\...
NULL 博文链接:https://zhangzhaoaaa.iteye.com/blog/2123021
this is function used for local histo gram equalization. you have to pass the window size
堆 概要 CheckHard.js是一个用于Javascript和Node.... import { isFunction , isArray } from "@dwtechs/checkhard" ; if ( isFunction ( variable ) ) { //variable is a function } if ( ! isArray ( variable , 2
isFunction 用途:判断是否是 function 类型 var p = function(){}; util.isFunction(p); ==>true isArray 用途:判断是否是一个数组 var p = []; util.isArray(p); ==>true isGlobal 用途:判断是否是全局变量 ...
该项目已转移到 ,现在称为 。... import { isFunction , isArray } from "@lcluber/chjs" ; if ( isFunction ( variable ) ) { //variable is a function } if ( ! isArray ( variable ) ) { //
angular.extend angular.equals angular.forEach angular.noop angular.bind angular.fromJson angular.identity angular.isUndefined angular.isDefined angular.isString angular.isFunction angular.isObject ...