简体   繁体   English

Javascript:处理常量和跨浏览器的兼容性

[英]Javascript: dealing with constants and cross-browser compatibility

Just read that ( const ) Behavior will vary across browser implementations . 只需了解一下const )行为会因浏览器实现而异 The answer I pointed to is pretty old (2012) so, is it still so bad? 我所指出的答案是相当古老的(2012年),那么,它仍然那么糟糕吗?

As I need cross browser compatibility in my project so should I avoid use of const keyword? 由于我的项目需要跨浏览器兼容性,因此应该避免使用const关键字吗?

If 'yes' - how do I replace one? 如果是,我该如何替换?

UPD: UPD:

Just checked the description on the developer.mozilla.org . 只需检查一下developer.mozilla.org上的描述。 And YES: looks like it is that bad... 是的:看起来很糟糕...

在此处输入图片说明

const isn't available in old browsers (I'm looking at you, IE, Safari). const在旧的浏览器中不可用(我在找您,IE,Safari)。

If you want to play safe, and you have no build step (Babel, TypeScript, etc), use only var . 如果您想安全玩,并且没有构建步骤(Babel,TypeScript等),请仅使用var Both Babel and TypeScript will compile your const s and let s to var s. 无论巴贝尔和打字稿将编译const S和let s到var秒。 So if you use any of those tools, you can use const and let freely. 所以,如果你使用任何这些工具,你可以使用constlet自由。

Be mindful that var is not strictly equivalent to either let or const . 请注意, var不严格等于letconst ( var is hoisted, and has a different scope). var被吊起,并且具有不同的作用域)。

Also, if you don't care about IE or Safari, you can pretty much use const without a build tool. 另外,如果您不关心IE或Safari,则无需构建工具即可使用const

You can just simply use var instead of const to support old browsers. 您可以简单地使用var而不是const来支持旧的浏览器。 (** remember var isn't equivalent to const to some extent) and to protect a var from being changed you can just put it inside a private scope instead of global. (**请记住, var在某种程度上不等同于const ),并且为了保护var不被更改,您可以将其放在私有范围内而不是全局范围内。 (so it'll work partly like a const ) (所以它会像const一样工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM