Javascript 适用于 IE11 的 ES6 代理 Polyfill
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45285992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ES6 Proxy Polyfill for IE11
提问by brillout
IE11 does not and will not implement ES2015 Proxy objects. Yet IE11's end of extended support is October 14, 2025.
IE11 没有也不会实现 ES2015 代理对象。然而,IE11 的扩展支持的结束日期是 2025 年 10 月 14 日。
Is there any way to polyfill Proxy objects for IE11? All other browsers support Proxy already.
有没有办法为 IE11 填充代理对象?所有其他浏览器都已经支持代理。
If yes then we would all be able to use it in production today. If not then we'll have to wait almost a decade...
如果是,那么我们今天都可以在生产中使用它。如果没有,那么我们将不得不等待近十年......
Edit: I'm asking specifically for IE11 as I know IE to usually have IE specific features that I'm often not aware of.
编辑:我特别要求 IE11,因为我知道 IE 通常具有我通常不知道的 IE 特定功能。
Edit2: I'm particularly interested in being able to implement a catch-all interceptor. Similar to __getattr__in Python. It only has to work in IE11.
Edit2:我对能够实现全能拦截器特别感兴趣。类似于__getattr__在 Python 中。它只需要在 IE11 中工作。
回答by Dolf Barr
Best you can get is github: GoogleChrome/proxy-polyfill
你能得到的最好的是github:GoogleChrome/proxy-polyfill
According to Babel docs:
根据Babel 文档:
Due to the limitations of ES5, Proxies cannot be transpiled or polyfilled.
由于 ES5 的限制,代理不能被转译或填充。
回答by George
There's quite a concise answer for this question on Quora
这个问题在 Quora 上有一个非常简洁的答案
Proxies require support on the engine level and it is not possible to polyfill Proxy.
Most major JS engines have yet to implement support. Check out the ECMAScript 6 compatibility table.
You may want to use Object.observe instead, possibly with polyfills for browsers other than Chrome, but even then the proposal has been withdrawn, and it has been announced it will be removed from Chrome in a future version.
代理需要引擎级别的支持,并且无法填充代理。
大多数主要的 JS 引擎尚未实现支持。查看ECMAScript 6 兼容性表。
您可能想改用 Object.observe,可能会为 Chrome 以外的浏览器使用 polyfill,但即便如此,该提案已被撤回,并且已宣布它将在未来版本中从 Chrome 中删除。
I personally haven't tried the Object.observe solution but it might be a good place to start.
我个人还没有尝试过 Object.observe 解决方案,但它可能是一个很好的起点。
Good luck!
祝你好运!
EDIT:Thank you to Matt Jensen in the comments for pointing out there is infact a way to polyfill some parts of ES6 Proxy using this package: github.com/GoogleChrome/proxy-polyfill
编辑:感谢 Matt Jensen 在评论中指出实际上有一种方法可以使用这个包来填充ES6 代理的某些部分:github.com/GoogleChrome/proxy-polyfill
AWESOME
惊人的
回答by Vladislav Ihost
Direct solution for polyfilling ES6 Proxy in environments without support this feature, of course is impossible - if storing some polyfill function info window.Proxy is meant. But if thinking this way, most modern features of ES6 can't be supported, because they will raise syntax error for old-version ECMAScript engine.
在不支持此功能的环境中直接使用 polyfilling ES6 Proxy 的解决方案当然是不可能的 - 如果存储一些 polyfill 函数信息 window.Proxy 的意思。但是如果这样想,ES6 的大多数现代特性都无法支持,因为它们会引发旧版 ECMAScript 引擎的语法错误。
That's why you should use transpiler, which perform preceding wrapping ES6 code into specific constructions, and then evaluate transformed code on old engine. In current case, just use one Babel plugin: https://www.npmjs.com/package/babel-plugin-proxy
这就是为什么你应该使用转译器,它执行预先将 ES6 代码包装成特定结构,然后在旧引擎上评估转换后的代码。在当前情况下,只需使用一个 Babel 插件:https: //www.npmjs.com/package/babel-plugin-proxy
Of course, while using this solution, you should configure Webpack to segregate target bundles for different client agents / browsers, depending on it's feature set discovery. See details here: https://gist.github.com/newyankeecodeshop/79f3e1348a09583faf62ed55b58d09d9
当然,在使用这个解决方案时,你应该配置 Webpack 来隔离不同客户端代理/浏览器的目标包,这取决于它的功能集发现。在此处查看详细信息:https: //gist.github.com/newyankeecodeshop/79f3e1348a09583faf62ed55b58d09d9

