Reputation: 283023
e.g.
const AES_256_CBC = Symbol();
const encHeaders = {
AES_256_CBC: "foo",
}
This of course doesn't work because the key is actually the literal string "AES_256_CBC"
.
Normally you'd just set your properties after initializing the object to an empty hash,
var encHeaders = {};
encHeaders[AES_256_CBC] = "foo";
But I can't do this and have encHeaders
be const
, can I?
Upvotes: 1
Views: 51
Reputation: 283023
Figured it out. You can do it with square brackets:
const AES_256_CBC = Symbol();
const encHeaders = Object.freeze({
[AES_256_CBC]: "foo",
});
console.log(encHeaders[AES_256_CBC]); // foo
The square bracket notation was also added in ES6.
Update: Amended to include Bergi's suggestion
Upvotes: 1
Reputation: 816760
But I can't do this and have
encHeaders
beconst
, can I?
Yes you can. const
has no impact on the value, it only affects the binding itself. That is, you cannot assign a new value to encHeaders
but you can certainly mutate the value itself (if it's mutable).
Upvotes: 3