Template literal propery names in object literals

# Alex Kodat (5 years ago)

Just curious. Is there a reason template literals are not allowed as property names in object literals? I can do:

let obj = {'foo bar': 1};

and

let obj = {"foo bar": 1};

but not

let obj = {foo bar: 1};

It doesn't seem that allowing the latter would present any syntactic problems and seems like almost an oversight that it's not allowed.

The main reason I ask is that we've gone completely over to using template literals for all our literals (why not?) and was surprised that we can't use a template literal as an object literal property name. Obviously, we can do:

let obj = {[foo bar]: 1};

And given that square brackets allow arbitrary expressions for propery names, it wouldn't seem that supporting template literals for object literal property names would not present any daunting implementation issues.

I guess I'd argue that the Principle of Least Astonishment and/or completeness suggests that JS should support this.

Sorry if this has been asked before but couldn't find anything in the archive.

Thanks


Alex Kodat Senior Product Architect Rocket Software t: +1 781 684 2294 • m: +1 315 527 4764 • w: www.rocketsoftware.com

================================ Rocket Software, Inc. and subsidiaries ■ 77 Fourth Avenue, Waltham MA 02451 ■ Main Office Toll Free Number: +1 855.577.4323 Contact Customer Support: my.rocketsoftware.com/RocketCommunity/RCEmailSupport Unsubscribe from Marketing Messages/Manage Your Subscription Preferences - www.rocketsoftware.com/manage-your-email-preferences Privacy Policy - www.rocketsoftware.com/company/legal/privacy-policy

This communication and any attachments may contain confidential information of Rocket Software, Inc. All unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify Rocket Software immediately and destroy all copies of this communication. Thank you.

# Gus Caplan (5 years ago)

Related discussion tc39/ecma262#1399

# Alex Kodat (5 years ago)

Thanks Gus,

Good stuff. Though I think I’d take a different tack on the discussion at that link, especially as I think the template literals should allow substitutions (why not?):

let obj = { ${Date()}: 1};

I guess the tack I would take in the spec would be to add TemplateLiteral to ComputedPropertyName and not worry about whether or not it's a NoSubstitutionTemplate.

Thanks


Alex Kodat Senior Product Architect Rocket Software t: +1 781 684 2294 • m: +1 315 527 4764 • w: www.rocketsoftware.com

From: Gus Caplan <me at gus.host>

Sent: Thursday, November 7, 2019 11:13 AM To: Alex Kodat <akodat at rocketsoftware.com>

Cc: es-discuss at mozilla.org Subject: Re: Template literal propery names in object literals

Related discussion nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Ftc39%2Fecma262%2Fissues%2F1399&data=02|01||9e20b9f578574ad67b1308d763a5b814|79544c1eed224879a082b67a9a672aae|0|1|637087435736614052&sdata=xDiwNTWHvdfIakrVFe6RmiP5pLHzC0i%2BoRNwKgvTMvc%3D&reserved=0

On Thu, Nov 7, 2019 at 8:46 AM Alex Kodat <mailto:akodat at rocketsoftware.com> wrote:

Just curious. Is there a reason template literals are not allowed as property names in object literals? I can do:

let obj = {'foo bar': 1};

and

let obj = {"foo bar": 1};

but not

let obj = {foo bar: 1};

It doesn't seem that allowing the latter would present any syntactic problems and seems like almost an oversight that it's not allowed.

The main reason I ask is that we've gone completely over to using template literals for all our literals (why not?) and was surprised that we can't use a template literal as an object literal property name. Obviously, we can do:

let obj = {[foo bar]: 1};

And given that square brackets allow arbitrary expressions for propery names, it wouldn't seem that supporting template literals for object literal property names would not present any daunting implementation issues.

I guess I'd argue that the Principle of Least Astonishment and/or completeness suggests that JS should support this.

Sorry if this has been asked before but couldn't find anything in the archive.

Thanks


Alex Kodat Senior Product Architect Rocket Software t: +1 781 684 2294 • m: +1 315 527 4764 • w: nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.rocketsoftware.com&data=02|01||9e20b9f578574ad67b1308d763a5b814|79544c1eed224879a082b67a9a672aae|0|1|637087435736623994&sdata=hTNIou6kPg5Ms%2FnIK%2BFVjrWa7mcGDtAbSJrVLVufxQU%3D&reserved=0

================================ Rocket Software, Inc. and subsidiaries ■ 77 Fourth Avenue, Waltham MA 02451 ■ Main Office Toll Free Number: +1 855.577.4323 Contact Customer Support: nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmy.rocketsoftware.com%2FRocketCommunity%2FRCEmailSupport&data=02|01||9e20b9f578574ad67b1308d763a5b814|79544c1eed224879a082b67a9a672aae|0|1|637087435736623994&sdata=FnuZYm9iu1AHZS4TNuA1TF95h0DzCh1HyF%2FN3CLoQ9M%3D&reserved=0 Unsubscribe from Marketing Messages/Manage Your Subscription Preferences - nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.rocketsoftware.com%2Fmanage-your-email-preferences&data=02|01||9e20b9f578574ad67b1308d763a5b814|79544c1eed224879a082b67a9a672aae|0|1|637087435736623994&sdata=xWVTW4Dr2qH1Hfc9woMu1ah6M9AzNcSkVot1y8A9Dmc%3D&reserved=0 Privacy Policy - nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.rocketsoftware.com%2Fcompany%2Flegal%2Fprivacy-policy&data=02|01||9e20b9f578574ad67b1308d763a5b814|79544c1eed224879a082b67a9a672aae|0|1|637087435736633951&sdata=X3vLto4DdTHU4roy6wY0Ep%2BXVucGx3v9Dw28KsPzfsg%3D&reserved=0

This communication and any attachments may contain confidential information of Rocket Software, Inc. All unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify Rocket Software immediately and destroy all copies of this communication. Thank you.

# Jordan Harband (5 years ago)

Anything dynamic - computed - should be in brackets, since that's what that indicates.

Thus, template literals with substitutions must require brackets.

Based on sentiments like tc39/ecma262#1399, either all template literals or none should be permitted in a given position.

Thus, no change is possible.