And even if we *could* get pointers into JS, I'd *strongly* not want
it to be like what's proposed here. Instead, I'd prefer an object
encapsulating a reference to a variable, something like this (although
engines could avoid the ceremony of closures here):

```js
let foo = 1;

func(ref foo, bar)
// Equivalent to:
func({deref: () => foo, set: v => foo = v}, bar)

function func(ref foo, bar) {
    foo += 2
}

// Equivalent to:
function func(foo) {
    foo.set(foo.deref() + 2)
}
```

I've found myself more than once wanting a way to manipulate a
variable by reference, in a way that's a bit more ergonomic than just
creating a single-property object. It doesn't need to be much, but it
doesn't need to be much. As a concrete example, recursive string
joining could just pass a shared reference instead of an object with a
single property:

```js
function recursiveJoinLoop(ref str, array, sep) {
    if (Array.isArray(array)) {
        for (const item of array) recursiveJoinLoop(ref str, item, sep)
    } else {
        if (str == null) str = ""
        str += String(array)
    }
}

function recursiveJoin(array, sep) {
    let str
    for (const item of array) recursiveJoinLoop(ref str, array, sep)
    return str
}
```
-----

Isiah Meadows
m...@isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Mon, Mar 19, 2018 at 5:48 PM, Michael J. Ryan <track...@gmail.com> wrote:
> Please no, mutable objects are bad enough imho.
>
> On Mon, Mar 19, 2018, 12:47 Sebastian Malton <sebast...@malton.name> wrote:
>>
>> Proposal:
>> Add a new variable-esk type called pointer which acts sort of like a
>> reference to the data which it has been assigned to but modifies also the
>> original reference when modified.
>>
>> Justification:
>> When working with objects in particular it is often useful for satiny
>> reasons to use local variables (mostly const's) as a shorthand reference
>> both for compactness and also so that you don't have to type out the entire
>> path every type. This would allow for non - objects but also overwriting
>> objects if wanted but still have this freedom and convenience.
>>
>> Method:
>> Add a new notation
>>
>> ```
>> let:prt name = obj.field.name.fullname;
>> ```
>>
>> Here if `name` is updated or if `obj.field.name.fullname` is updated so is
>> the other.
>>
>> Sebastian Malton
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to