[英]How to replace javascript standard built-in objects?
My client builds their own app based on Chromium, their navigator.appVersion is AppleWebkit/534+我的客户基于 Chromium 构建他们自己的应用程序,他们的 navigator.appVersion 是AppleWebkit/534+
But to my surprise they replace javascript standard built-in objects with their own, for example, check their Map bellow,但令我惊讶的是,他们用自己的替换了 javascript 标准内置对象,例如,检查他们的 Map bellow,
Their Map has methods like,他们的 Map 有这样的方法,
arr:Array[0]
isEmpty:function
remove:function
But no standard Map method like has, keys, values
, check here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map (BTW, the reason we need Map here instead of plain js object is we need the key to be something other than string)但是没有像has, keys, values
这样的标准 Map 方法,请在此处查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map (顺便说一句,我们在这里需要 Map 而不是简单的 js 对象是我们需要密钥是字符串以外的东西)
I am curious, how did they do that?我很好奇,他们是怎么做到的? Why would someone want to do that!!为什么有人要这么做!!
And more importantly how do I restore the build in version ?更重要的是,我如何恢复构建版本?
I had the same issue that the global function has been overwritten.我遇到了同样的问题,即全局函数已被覆盖。
My trick is to use the contentWindow
of an iframe
to get the original function back:我的技巧是使用iframe
的contentWindow
来获取原始函数:
Here's the code, it doesn't work in code-snippets of StackOverflow due to iframe policy, but it should work with your environment:这是代码,由于 iframe 策略,它在 StackOverflow 的代码片段中不起作用,但它应该适用于您的环境:
// contaminated Map
Map = function() {
this.foo = 'bar';
}
const map1 = new Map();
console.log(map1);
// restoring Map
const iframe = document.createElement('iframe');
document.body.append(iframe);
const OriginalMap = iframe.contentWindow.Map;
iframe.remove();
const map2 = new OriginalMap();
map2.set('foo', 'bar');
console.log(map2);
Here's the screenshot shows that it works:这是屏幕截图显示它有效:
This is more like a workaround, not sure if there's a better way to do it.这更像是一种解决方法,不确定是否有更好的方法。
how did they do that?他们是怎么做到的?
It's as simple as:这很简单:
Map = function() { ... }
Why would someone want to do that!!为什么有人要这么做!!
As other comments have guessed, probably because the code is older than the availability of built-in Map
.正如其他评论所猜测的那样,可能是因为代码比内置Map
的可用性更旧。 If the same code is also supposed to run on older browsers, then this consideration might still be relevant.如果同样的代码也应该在旧浏览器上运行,那么这个考虑可能仍然相关。
how do I restore the build in version ?如何恢复构建版本?
The suggestion in the other answer is an interesting idea... I can't think of a better way.另一个答案中的建议是一个有趣的想法......我想不出更好的方法。
That said: I'm guessing your task is to make extensions/modifications to an existing codebase.也就是说:我猜您的任务是对现有代码库进行扩展/修改。 Having two different Map
s in the same codebase sounds pretty confusing -- for anyone working on this code after you (which may be yourself, in a couple of months), it'll be pretty hard to understand what's going on.在同一个代码库中有两个不同的Map
听起来很令人困惑——对于在您之后处理此代码的任何人(可能是您自己,几个月后),将很难理解发生了什么。 So I offer another suggestion: don't just locally get the built-in Map
, and instead pick either of:所以我提供另一个建议:不要只在本地获取内置Map
,而是选择以下任一项:
Map
s.使整个系统现代化以使用内置的 JavaScript Map
s。 Replace all uses of the handcrafted Map, and then delete that definition.替换手工制作的 Map 的所有用途,然后删除该定义。 Use built-in Map
everywhere.随处使用内置Map
。 (Check with your client though whether they are okay with you spending your time on this.) (请咨询您的客户,但他们是否同意您在这方面花费时间。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.