Dash-case keys

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Dash-case keys

Sultan
Is there any reason that dash-case keys are not supported in the object literal syntax.

For example:

const style = {
    font-size: 10
}

Compared to what one needs to do today:

const style = {
    'font-size': 10
}

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Jordan Harband
is that a key called "font-size" or the subtraction of `size` from `font`?

On Mon, Jan 28, 2019 at 1:48 PM Sultan <[hidden email]> wrote:
Is there any reason that dash-case keys are not supported in the object literal syntax.

For example:

const style = {
    font-size: 10
}

Compared to what one needs to do today:

const style = {
    'font-size': 10
}
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Sultan
The former as the following is equally invalid syntax errors:

const font = 1
const size = 1

const a = {
        font-size: 10
}
const b = {
        font+size: 10
}
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Jordan Harband
i think if i can use something unquoted in an object literal, i'd expect to be able to use it in dot access - ie, `obj.font-size` - and then that problem arises.

On Mon, Jan 28, 2019 at 3:43 PM Sultan Tarimo <[hidden email]> wrote:
The former as the following is equally invalid syntax errors:

const font = 1
const size = 1

const a = {
        font-size: 10
}
const b = {
        font+size: 10
}

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Sultan
That might have been the case, but the expectation that an unquoted form should equally have a quoted form or vice-versa is not an issue in practice given the presence of a similar but opposite semantic in the private sigil access notation proposal: object.#validPrivateSigil -> object[‘#invalidPrivateSigil’].

In kind, this would exploit the same grammar affordances to be born from this.

That is obj.font-size would rightly be invalid. The more common interfacing with this notation would be through iterators. That is:

for (var key in object) {
    style.setProperty(key, object[key])
}

The addition of which would see to improve the ergonomics with using dash-case keys common in the CSS grammar within the object literals syntax.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Claude Pache


Le 29 janv. 2019 à 08:45, Sultan Tarimo <[hidden email]> a écrit :

That is obj.font-size would rightly be invalid. 

Sadly, `obj.font-size` is *not* invalid, it has some well-defined semantics. That would be a source of bugs

 —Claude

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Sultan
This is expected and by design. For example obj[‘#invalidPrivateSigil’] is also not invalid. The affordances would be the same.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

Jordan Harband
There's a difference, though, between private field access - an entirely distinct and new kind of thing - and normal property access, a well-established and understood thing.

On Mon, Jan 28, 2019 at 11:53 PM Sultan Tarimo <[hidden email]> wrote:
This is expected and by design. For example obj[‘#invalidPrivateSigil’] is also not invalid. The affordances would be the same.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Dash-case keys

T.J. Crowder-2
On Tue, Jan 29, 2019 at 7:45 AM Sultan Tarimo
<[hidden email]> wrote:
> That is obj.font-size would rightly be invalid.

By which I think you mean, would still be `obj.font - size` with its current meaning.

I don't think the confusion that causes is worth the benefit of not putting the name in quotes in the object initializer. Particularly not with the well-established convention of using camelCase in that situation when interoperating with objects that have (or act like they have) dash-named properties (like the CSSStyleDeclaration object on HTML elements).

-- T.J. Crowder

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss