例如,我在某个网站上加载了一个脚本,我想知道JSON.parse/stringify是否没有修补猴子.
我注意到如果我在Chrome/FF中的函数上使用toString JSON.stringify.toString,那么我会回来:
function stringify() {
[native code]
}
Run Code Online (Sandbox Code Playgroud)
我的问题是你认为这是验证一个函数是否被猴子修补的好方法吗?也很想听听这个问题的任何其他方法.
是的,这是检查本机函数是否已被覆盖的唯一实用方法。
const isNative = fn => !!fn.toString().match(/\[native code\]/)
console.log(isNative(JSON.stringify));
Run Code Online (Sandbox Code Playgroud)
更强大的解决方案可以使用Function.prototype.toString()而不是直接调用fn.toString(),但两者都是可猴子修补的。JavaScript 的乐趣:)
人们很容易假装 JSON.stringify.toString
JSON.stringify = function() {}
JSON.stringify.toString = function() {return 'ha-ha'}
console.log(JSON.stringify); //ha-ha
Run Code Online (Sandbox Code Playgroud)
一种更强大的方式是使用 Function.prototype.toString
Function.prototype.toString.call(JSON.stringify)
Run Code Online (Sandbox Code Playgroud)
但真正糟糕的monkeypatcher也可以补丁Function.prototype.toString:)
| 归档时间: |
|
| 查看次数: |
350 次 |
| 最近记录: |