secrets of the javascript Ninja(Function Type)(javascript忍者的秘密)
2009-03-30 22:25
423 查看
大多数情况下,typeof就可以完成检查函数类型的工作,例如:
但是根据浏览器的不同,我们可以发现以下集中情况
在firefox2和firefox3中,如果检测一个<object/>类型的元素,将会检测出来是一个function
firefox2正则表达式会被认为是一个function;但是在firefox3中会被认为是object
IE6和IE7中一些dom元素的方法会被认为一个object
//typeof /test/=='function'
safari3中认为NodeList为function如:typeof document.body.childNodes =="function"
纵观以上所有情况,我们可以写出一个通用的函数来检测某个值是否是函数
function ninja(){} alert(typeof ninja);
但是根据浏览器的不同,我们可以发现以下集中情况
在firefox2和firefox3中,如果检测一个<object/>类型的元素,将会检测出来是一个function
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <mce:script type="text/javascript"><!-- window.onload=function(){ var o=document.getElementById("doc_html"); alert(typeof o); } // --></mce:script> </head> <body> <object id="doc_html" name="doc_html" style="LEFT: 0px; TOP: 0px" data=" gledit.htm" width=530 height=320 type=text/x-scriptlet VIEWASTEXT></object> </body> </html>
firefox2正则表达式会被认为是一个function;但是在firefox3中会被认为是object
IE6和IE7中一些dom元素的方法会被认为一个object
//typeof /test/=='function'
safari3中认为NodeList为function如:typeof document.body.childNodes =="function"
纵观以上所有情况,我们可以写出一个通用的函数来检测某个值是否是函数
function isFunction( fn ) { return !!fn && !fn.nodeName && fn.constructor != String && fn.constructor != RegExp && fn.constructor != Array && /function/i.test( fn + "" ); }
相关文章推荐
- secrets of the javascript Ninja( javascript Timer)(javascript忍者的秘密)
- 翻译 Secrets of the JavaScript Ninja (JavaScript忍者禁术)
- secrets of the javascript Ninja( with(){} 的用法)(javascript忍者的秘密)
- secrets of the javascript Ninja (Function Prototypes)(javascript忍者的秘密)
- How to get the object type in javascript:
- javascript Function.propertype.bind 解析
- 翻译 Secrets of the JavaScript Ninja - 4.挥舞函数(4.Wielding functions)
- 关于JavaScript一个错误 Uncaught TypeError: form.submit is not a function
- Is the type of “pointer-to-member-function” different from “pointer-to-function”?
- [SharePoint]javascript client object model 获取lookup 类型的field的值,包括user类型(单人或者多人)的值。how to get the multiple user type/lookup type field value by Javascript client object model
- PowerTip of the Day-Acessing Function Parameters by Type
- javascript 错误 "Uncaught TypeError: string is not a function"
- 对象functionJavaScript: The Definitive Guide 权威指南,读书笔记(一)
- secret of the javascript ninja笔记
- 翻译 Secrets of the JavaScript Ninja - 5.闭包 (5.Closing in on closures)
- Using a Comparison Function for the Key Type
- [在读]Secrets of the javascript Ninja
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- javascript 报错Uncaught TypeError: {(intermediate value)(intermediate value)} is not a function
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.