使用JavaScript检测函数是否为本机代码

Every once a while I'll test is a given function is native code -- it's an important part of feature testing whether a function was provided by the browser or via a third party shim which acts like the native feature.  The best way to detect this, of course, is evaluating the toString return value of the function.

我会每隔一段时间测试给定的功能是本机代码-这是功能测试的重要组成部分,是测试功能是由浏览器提供还是通过行为类似于本机功能的第三方填充程序提供。 当然,检测此问题的最佳方法是评估函数的toString返回值。

JavaScript (The JavaScript)

The code to accomplish this task is fairly basic:

完成此任务的代码非常基本:


function isNative(fn) {
	return (/\{\s*\[native code\]\s*\}/).test('' + fn);
}


Converting to the string representation of the function and performing a regex match on the string is how it's done.  There isn't a better way of confirming a function is native code!

转换为函数的字符串表示形式并对该字符串执行正则表达式匹配的方法是完成的。 没有更好的方法来确认函数是本机代码!

更新! (Update!)

Lodash creator John-David Dalton has provided a better solution:

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;
}());


So there you have it -- a better solution for detecting if a method is native. Of course you shouldn't use this as a form of security -- it's only to hint toward native support!

这样就可以了-一种更好的解决方案,用于检测方法是否是本机的。 当然,您不应该将其用作安全性形式-只是暗示对本机支持!

翻译自: https://davidwalsh.name/detect-native-function

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值