最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is it impossible to assign constant properties of JavaScript objects using the const keyword? - Stack Overflow

programmeradmin2浏览0评论

First, I had asked is it possible: How to create Javascript constants as properties of objects using const keyword?

Now, I gotta ask: why? The answer to me seems to be 'just because', but it would be so useful to do something like this:

var App = {};  // want to be able to extend
const App.goldenRatio= 1.6180339887  // throws Exception

Why can constants set on an activation object work but not when set on they are set on any other?

What sort of damage can be done if this were possible?

What is the purpose of const, if not to prevent a public API from being altered?

First, I had asked is it possible: How to create Javascript constants as properties of objects using const keyword?

Now, I gotta ask: why? The answer to me seems to be 'just because', but it would be so useful to do something like this:

var App = {};  // want to be able to extend
const App.goldenRatio= 1.6180339887  // throws Exception

Why can constants set on an activation object work but not when set on they are set on any other?

What sort of damage can be done if this were possible?

What is the purpose of const, if not to prevent a public API from being altered?

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Jun 1, 2012 at 2:51 danronmoondanronmoon 3,8735 gold badges35 silver badges58 bronze badges 3
  • 5 It's not that damage could be done. Certainly many languages offer that construct. It's that JavaScript does not offer that construct. – Eric J. Commented Jun 1, 2012 at 2:53
  • 1 By what I can understand here, const is not standard, even if some browsers can use it (not IE). I've never had the case where i needed this in JS anyway... You can still follow the convention of ALL UPPER CASE and most devs will understand that is a constant. – elclanrs Commented Jun 1, 2012 at 3:08
  • Is philosophy of language design now in scope for Stack Overflow? There are lots of reasons why one might omit such features from a language, but I suspect in this case it's because the language was rather hurriedly rushed out the door, with as much Java-y syntax thingies attached as possible to tie the two otherwise-related languages together for marketing purposes. – Mark Reed Commented Jun 1, 2012 at 3:09
Add a ment  | 

4 Answers 4

Reset to default 4

If you want an unchangeable value in a modern browser, use defineProperty to create an unwritable (aka read-only) property:

var App = {};
Object.defineProperty(App,'goldenRatio',{value:1.6180339887});
console.log( App.goldenRatio ); // 1.6180339887
App.goldenRatio = 42;
console.log( App.goldenRatio ); // 1.6180339887
delete App.goldenRatio;         // false
console.log( App.goldenRatio ); // 1.6180339887

If you don't pass writable:true in the options to defineProperty it defaults to false, and thus silently ignores any changes you attempt to the property. Further, if you don't pass configurable:true then it defaults to false and you may not delete the property.

Apart from the fact that const is and not supported cross-browser (it's an ES6 feature), const App.goldenRatio= 1.6180339887 doesn't work for the same reason var App.goldenRatio= 1.6180339887 doesn't: you're setting an object property, so it's a syntax error to prepend it with a var or const keyword.

Going to answer the question you meant to ask and say never use const. The interpreter doesn't do anything with it. All it does is mislead the developer into thinking s/he can assume that the value never changes, which is about as likely as if the const keyword weren't present.

var MY_CONSTANT = "some-value"; You can use conventions like ALL_CAPS to show that certain values should not be modified

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论