JavaScript object notation shorthand

I saw this syntax in JavaScript and thought it might be some feature from TypeScript or some other higher level layer:

let text = "Hello World"
let state = { fruit: "banana" }

let obj = { text, state }

But it turns out in plain JavaScript that now does the same thing as:

let obj = { text: "Hello World", state: { fruit: "banana" } }

In fact they also have "computed property names":

let prop = 'foo';
let o = {
    [prop]: 'hey',
    ['b' + 'ar']: 'there'
}

Object initializer - JavaScript | MDN

Ren-C is inches away from doing the computed property names in MAKE OBJECT! (using SET-GROUP! rather than SET-BLOCK! would be more appropriate).

But because of the freeform nature of the code block in MAKE OBJECT!, the appearance of a WORD! alone can't indicate an embedded field.

It may be that there should be a less freeform alternative to MAKE OBJECT! which doesn't let you put arbitrary code in the middle of it. Or at the very least, you put that code in GROUP!s.

Perhaps MAKE OBJECT! itself should have that requirement. It could be like PARSE and just ignore the GROUP!s as it goes.

1 Like

In understanding JavaScript more from a Rebol-brain (or even just with JSON priors), I had to unlearn the idea that [ ] and { } notation were literals; rather they are constructors. I think this distinction is why JavaScript is at best awkwardly Lispy. Also constructors can't self-reference so e.g. within an object you can't reference a prior key set in the same construction ({'a': 123, 'b': < no way to get 'a' >}), although one 'benefit' to this is you can't inadvertently create a circularly referenced structure.

I do like the constructors in principle, but again in Rebol-land my inclination is to do this with language rather than syntax—the question is given how much heavy lifting syntax does in constructors, how much language/convention would need to be used to get some a degree of parity without being overly verbose:

[
    1, 2, [3, 4], ...[5, 6], {
        ["a" + 123]: "A value",
        anotherKey,
        ...anotherMap
    }
]

constructor block! [
    1 2 [3 4] spread [5 6] constructor map! [
        combine ["a" 123] "A value"
        from another-key
        spread another-map
    ]
]

I don't suppose it's a trivial exercise.

This sounds like CONSTRUCT as was.

1 Like

I am not completely settled on my ideas about what I have called "fences":

{ Rethinking Braces }... as an array type?

The idea that braces evaluate to unstable fence isotopes and then decay into objects is so strange that I want to try it just to know what that might be like (!)

In essence, there's already three BLOCK! constructors in traditional Rebol (REDUCE, COMPOSE and COLLECT) of which COLLECT is perhaps the most versatile. It does require KEEP for most values which is likely why it's not the only one, but gives you access to all of the other language features:

; alt-ARRAY
;
collect [
    repeat value 4 [
        keep value
    ]
]

A few variations on KEEP would be similarly useful in OBJECT! and MAP! creation:

collect-map [
    keep "key" "value"
    spread another-map
]

collect-object [
    keep foo: "Bar"
    keep ["a" 123] "A value"
    spread another-object
]

I do like the idea of implicit collection though as well. I don't know if it's at all possible to mix implicit/explicit collection in any sane way.


This would completely mess with any trivial noodling around with the language, but you could use A in place of CONSTRUCTOR in the previous posting:

a block! [
    1 2 [3 4] spread [5 6] a map! [
        combine ["a" 123] "A value"
        from another-key
        spread another-map
    ]
]
1 Like