如何确保浏览器能够支持 **{get X() {}}**
我拒绝使用 __defineProperty__
而是更喜欢替代语法 {get X() {}}
但这在 IE 上不起作用。
除了浏览器检测之外,我可以检测浏览器支持较新语法的最佳方法是什么?
编辑:好吧,实际上我并没有尝试特别检测 IE,而是将那些“不支持 get X(){} 语法的浏览器”重定向到 notsupported.html。我相信有某种方法可以做到这一点,并且正在努力解决它,但万一有人之前已经遇到过这个问题并有解决方案。
编辑2:顺便说一句,这并不意味着没有人(除了我之外)使用 < strong>get X(){} 语法,因为并非所有浏览器都支持该语法(或者尚未得到 5 个主要浏览器的支持)?
i refuse to use __defineProperty__
and instead prefer the alternative syntax {get X() {}}
However this will not work on IE.
Aside from browser detection, what is the best way i can detect that a browser supports the newer syntax?
Edit: ok actually im not trying to detect IE in particular but redirect those "browsers that do not support get X(){} syntax" to notsupported.html. I believe that there's some way to do it and am working on it but in case someone already has this problem before and had a solution..
Edit 2: btw doesn't that mean that no one (erm other than me) uses the get X(){} syntax since its not supported by all (or not supported by the 5 major browsers yet) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所指出的,您不能强迫较旧的(当前的!)浏览器接受较新的语法。您会如何处理浏览器检测?对于旧浏览器使用旧语法,否则使用新语法?那么你就需要把同样的代码写两次。
确定您需要支持的一组浏览器,确定它们可以向您保证哪些功能,然后限制自己使用这些功能。这就是网络开发的工作原理。
As others have noted, you cannot force older (current!) browsers to accept newer syntax. And what would you do with browser detection? Use the old syntax for the old browsers and new syntax otherwise? Then you're writing the same code twice.
Decide on a set of browsers you need to support, determine what features they can all guarantee to you, then limit yourself to those features. That's how web development works.
您想要使用不常用的 ES5 功能。
您想要使用常见 JavaScript 解释器无法识别的语法。
没有办法模仿它。
我建议您只使用
如果您检查基准。然后你会发现使用 ES5 慢了 15 倍。坚持使用 ES3 即可。
您对此无能为力,因为无法在 IE8 中模拟 getter。
You want to use an ES5 feature that is not commonly implemented.
You want to use syntax that common javascript interpreters cannot recognise.
There is no way to emulate it.
I recommend you just use
If you check the benchmark. Then you'll see using ES5 is 15 times slower. Just stick with ES3.
There is little you can do about this as there is no way to emulate getters in IE8.
试试这个:
Try this: