isFunction

[size=xx-large]isFunction[/size]

[size=large]标准写法是:[/size]

var isFunction = function(obj) {
return Object.prototype.toString.call(obj) === '[object Function]'
}


[size=large]深层次:[/size]
[list]
[*]有博客说, 之前的chrome会识别 new Regexp()为function, 没有测试之前的chrome, 现在的不会了
[*]有博客说, document.write在IE下会识别为object, 其实新的写法, 依然还是object, 不信,jquery测试下, 同理getAttribute也是一样的
[*]jquery的isFunction注释
[code]
// 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";
}
[/code]
[*]jquery中的 type(obj), 即是 Object.prototype.toString.call(obj)
[*]jquery已经不支持DOM方法,以及像alert这样的方法, 因为他们在IE都返回false,无法区分
[*]#2968 见 [url]http://bugs.jquery.com/ticket/2968[/url]
[/list]


[size=large]最简单的写法:[/size]

var isFunction = function(obj) {
return typeof obj === 'function'
}


[*] 这个写法, 在jquery的isFunction测试用例中, 全部通过 [url]https://code.google.com/p/jqueryjs/source/browse/trunk/jquery/test/unit/core.js[/url]
[*] 简单的就是最好的~~


[size=large]测试用例 (修改自jquery的isFunction测试用例)[/size]

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值