5,006
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
window.onload = function() {
document.getElementById("info").innerHTML = "typeof undefined == " + (typeof undefined) + //"undefined"
"<br/>typeof null === " + (typeof null) + //"object"
"<br/>typeof true === " + (typeof true) + //"boolean"
"<br/>typeof 1 === " + (typeof 1) + //"number"
"<br/> typeof \"hello\" === " + (typeof "hello") + //"string"
"<br/> typeof new Date() === " + (typeof new Date()) + //"object"
"<br/> typeof (new Date()).getDate === " + (typeof (new Date()).getDate) + //"function"
"<br/> typeof document.getElementById === " + (typeof document.getElementById) + //"function"
"<br/> typeof document === " + (typeof document); //"object"
}
</script>
<div id="info"></div>
以上测试用例,测试的是各种数据类型的typeof表达式结果。function A(){
}
var a=new A();
alert(type of a);//object
<script type="text/javascript">
function isFunction( fn ) {
return !!fn && //不为空,存在
!fn.nodeName && //不是节点对象
fn.constructor != String && //不是字符串类型
fn.constructor != RegExp && //不是正则表达式
fn.constructor != Array && //不是数组
/function/i.test( fn + "" ); //toString()中包含"function"
}
alert(isFunction(document.getElementById)); //true
</script>
以上是大师John Resig判断是否Function的方法。很经典。利用的是排除法,聪明……佩服……function test(){}
function isFunction(obj)
{
return typeof(obj) == 'function';
}
alert(isFunction(test));
<script>
function isFunction(handler) {
return Object.prototype.toString.call(handler) === "[object Function]";
}
function test() {}
var arr = "123";
alert(isFunction(test));
alert(isFunction(document.getElementById));
alert(isFunction(arr));
</script>