简体   繁体   中英

Javascript: dealing with constants and cross-browser compatibility

Just read that ( const ) Behavior will vary across browser implementations . The answer I pointed to is pretty old (2012) so, is it still so bad?

As I need cross browser compatibility in my project so should I avoid use of const keyword?

If 'yes' - how do I replace one?

UPD:

Just checked the description on the 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).

If you want to play safe, and you have no build step (Babel, TypeScript, etc), use only var . Both Babel and TypeScript will compile your const s and let s to var s. So if you use any of those tools, you can use const and let freely.

Be mindful that var is not strictly equivalent to either let or const . ( var is hoisted, and has a different scope).

Also, if you don't care about IE or Safari, you can pretty much use const without a build tool.

You can just simply use var instead of const to support old browsers. (** 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. (so it'll work partly like a const )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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