检测一个函数是否是JavaScript原生函数的小技巧
作者:bea
在我的开发工作中经常会遇到需要判断一个函数是否是JavaScript原生函数的情况,有时候这是一个很必要的工作,你需要知道这个函数是浏览器自身提供的,还是由第三方封装、伪装成原生函数。当然,最好的方法是考察执行这个函数的toString方法的返回值。 The JavaScript 完成这个任务的方法非常简单: 代码如下: function isNative(fn) { return (/{s*[native code]s*}/).test('' + fn); }
在我的开发工作中经常会遇到需要判断一个函数是否是JavaScript原生函数的情况,有时候这是一个很必要的工作,你需要知道这个函数是浏览器自身提供的,还是由第三方封装、伪装成原生函数。当然,最好的方法是考察执行这个函数的toString方法的返回值。
The JavaScript
完成这个任务的方法非常简单:
代码如下:
function isNative(fn) {
return (/{s*[native code]s*}/).test('' + fn);
}
toString方法会返回这个方法的字符串形式,然后用正则表达式判断里面包含的字符。
更强悍的方法
Lodash的创始人John-David Dalton找到了一个更佳的方案:
代码如下:
;(function() {
// Used to resolve the internal `[[Class]]` of values var toString = Object.prototype.toString; // Used to resolve the decompiled source of functions var fnToString = Function.prototype.toString; // Used to detect host constructors (Safari > 4; really typed array specific) var reHostCtor = /^[object .+?Constructor]$/;
// Compile a regexp using a common native method as a template. // We chose `Object#toString` because there's a good chance it is not being mucked with. var reNative = RegExp('^' + // Coerce `Object#toString` to a string String(toString) // Escape any special regexp characters .replace(/[.*+?^${}()|[]/\]/g, '\$&') // Replace mentions of `toString` with `.*?` to keep the template generic. // Replace thing like `for ...` to support environments like Rhino which add extra info // such as method arity. .replace(/toString|(function).*?(?=\()| for .+?(?=\])/g, '$1.*?') + '$' ); function isNative(value) { var type = typeof value; return type == 'function' // Use `Function#toString` to bypass the value's own `toString` method // and avoid being faked out. ? reNative.test(fnToString.call(value)) // Fallback to a host object check because some environments will represent // things like typed arrays as DOM methods which may not conform to the // normal native pattern. : (value && type == 'object' && reHostCtor.test(toString.call(value))) || false; } // export however you want module.exports = isNative; }());
现在你也看到了,很复杂,但更强大。当然,这不是为了做安全防护,它只是给你提供是否是原生函数的相关信息。
有用 | 无用
The JavaScript
完成这个任务的方法非常简单:
代码如下:
function isNative(fn) {
return (/{s*[native code]s*}/).test('' + fn);
}
toString方法会返回这个方法的字符串形式,然后用正则表达式判断里面包含的字符。
更强悍的方法
Lodash的创始人John-David Dalton找到了一个更佳的方案:
代码如下:
;(function() {
// Used to resolve the internal `[[Class]]` of values var toString = Object.prototype.toString; // Used to resolve the decompiled source of functions var fnToString = Function.prototype.toString; // Used to detect host constructors (Safari > 4; really typed array specific) var reHostCtor = /^[object .+?Constructor]$/;
// Compile a regexp using a common native method as a template. // We chose `Object#toString` because there's a good chance it is not being mucked with. var reNative = RegExp('^' + // Coerce `Object#toString` to a string String(toString) // Escape any special regexp characters .replace(/[.*+?^${}()|[]/\]/g, '\$&') // Replace mentions of `toString` with `.*?` to keep the template generic. // Replace thing like `for ...` to support environments like Rhino which add extra info // such as method arity. .replace(/toString|(function).*?(?=\()| for .+?(?=\])/g, '$1.*?') + '$' ); function isNative(value) { var type = typeof value; return type == 'function' // Use `Function#toString` to bypass the value's own `toString` method // and avoid being faked out. ? reNative.test(fnToString.call(value)) // Fallback to a host object check because some environments will represent // things like typed arrays as DOM methods which may not conform to the // normal native pattern. : (value && type == 'object' && reHostCtor.test(toString.call(value))) || false; } // export however you want module.exports = isNative; }());
现在你也看到了,很复杂,但更强大。当然,这不是为了做安全防护,它只是给你提供是否是原生函数的相关信息。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Jquery插件实现点击获取验证码后60秒内禁止重新获取
- node.js [superAgent] 请求使用示例
- JavaScript中逗号运算符介绍及使用示例
- JavaScript原生对象之String对象的属性和方法详解
- JQuery报错Uncaught TypeError: Illegal invocation的处理方法
- JavaScript原生对象之Number对象的属性和方法详解
- JavaScript原生对象之Date对象的属性和方法详解
- innerHTML属性,outerHTML属性,textContent属性,innerText属性区别详解
- JavaScript内存管理介绍
- JavaScript中的this关键字使用方法总结
- javascript中slice(),splice(),split(),substring(),substr()使用方法
- 在linux中使用包管理器安装node.js
- JQuery中serialize() 序列化
- JavaScript中通过prototype属性共享属性和方法的技巧实例
- JQuery中$.each 和$(selector).each()的区别详解
- JavaScript实现判断图片是否加载完成的3种方法整理
- JavaScript检查弹出窗口是否被阻拦的方法技巧
- JavaScript中常用的六种互动方法示例
- jQuery.position()方法获取不到值的安全替换方法