Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

javascript - JavaScript中是否有常量?(Are there constants in JavaScript?)

Is there a way to use constants in JavaScript?(有没有办法在JavaScript中使用常量?)

If not, what's the common practice for specifying variables that are used as constants?(如果没有,指定用作常量的变量的常见做法是什么?)

  ask by fuentesjr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since ES2015 , JavaScript has a notion of const :(从ES2015开始 ,JavaScript有一个const的概念:)

const MY_CONSTANT = "some-value";

This will work in pretty much all browsers except IE 8, 9 and 10 .(除了IE 8,9和10之外,几乎适用于所有浏览器 。)

Some may also need strict mode enabled.(有些可能还需要启用严格模式 。)

You can use var with conventions like ALL_CAPS to show that certain values should not be modified if you need to support older browsers or are working with legacy code:(您可以将var与ALL_CAPS等约定一起使用,以显示如果您需要支持旧版浏览器或使用旧代码,则不应修改某些值:)

var MY_CONSTANT = "some-value";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...