Syntax operator for "default assignment if value doesn't exits"
# Rob Ede (6 years ago)
pretty succinct with existing de-structuring syntax:
const [variable = defaultValue] = [maybeUndefinedValue]
const fn = ({ key = defaultValue }) => { console.log(key); }
pretty succinct with existing de-structuring syntax: ``` const [variable = defaultValue] = [maybeUndefinedValue] const fn = ({ key = defaultValue }) => { console.log(key); } ``` > On 11 Jan 2019, at 12:00, es-discuss-request at mozilla.org wrote: > > Send es-discuss mailing list submissions to > es-discuss at mozilla.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.mozilla.org/listinfo/es-discuss > or, via email, send a message with subject or body 'help' to > es-discuss-request at mozilla.org > > You can reach the person managing the list at > es-discuss-owner at mozilla.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of es-discuss digest..." > Today's Topics: > > 1. Syntax operator for "default assignment if value doesn't > exits" (Sultan) > > From: Sultan <thysultan at gmail.com> > Subject: Syntax operator for "default assignment if value doesn't exits" > Date: 11 January 2019 at 10:52:12 GMT > To: es-discuss <es-discuss at mozilla.org> > > > An operator syntax for the the "typeof" pattern used to detect if a environment/object has a value: > > if (typeof variable === 'undefined') {...} > if (typeof object.key === 'undefined') {...} > > This could manifest in destructuring as the following > > var fn = ({ key ||= 1 }) => { > } > > And with variables as: > > var global ||= {} > > Equivalent code: > > (arg) => { > if (typeof arg.key === 'undefined') { > arg.key = 1 > } > } > > if (typeof global === 'undefined') { > var global = {} > } > > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190112/85e1680f/attachment.html>
# Sultan (6 years ago)
const [variable = defaultValue] = [maybeUndefinedValue]
This would fail throw for variables you don't know exist i.e in the following example polyfill will throw if Symbol doesn't exist.
const [Symbol = SymbolPolyfill] = [Symbol]
This is specifically why convention is to use:
typeof maybeUndefinedValue === 'undefined'
const fn = ({ key = defaultValue }) => { console.log(key); }
The second example doesn't assign the key prop to the passed object.
>const [variable = defaultValue] = [maybeUndefinedValue] This would fail throw for variables you don't know exist i.e in the following example polyfill will throw if Symbol doesn't exist. const [Symbol = SymbolPolyfill] = [Symbol] This is specifically why convention is to use: typeof maybeUndefinedValue === 'undefined' >const fn = ({ key = defaultValue }) => { console.log(key); } The second example doesn't assign the key prop to the passed object. On Sat, Jan 12, 2019 at 5:54 PM Rob Ede <robjtede at icloud.com> wrote: > pretty succinct with existing de-structuring syntax: > > ``` > const [variable = defaultValue] = [maybeUndefinedValue] > > const fn = ({ key = defaultValue }) => { console.log(key); } > ``` > > On 11 Jan 2019, at 12:00, es-discuss-request at mozilla.org wrote: > > Send es-discuss mailing list submissions to > es-discuss at mozilla.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.mozilla.org/listinfo/es-discuss > or, via email, send a message with subject or body 'help' to > es-discuss-request at mozilla.org > > You can reach the person managing the list at > es-discuss-owner at mozilla.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of es-discuss digest..." > Today's Topics: > > 1. Syntax operator for "default assignment if value doesn't > exits" (Sultan) > > *From: *Sultan <thysultan at gmail.com> > *Subject: **Syntax operator for "default assignment if value doesn't > exits"* > *Date: *11 January 2019 at 10:52:12 GMT > *To: *es-discuss <es-discuss at mozilla.org> > > > An operator syntax for the the "typeof" pattern used to detect if a > environment/object has a value: > > if (typeof variable === 'undefined') {...} > if (typeof object.key === 'undefined') {...} > > This could manifest in destructuring as the following > > var fn = ({ key ||= 1 }) => { > } > > And with variables as: > > var global ||= {} > > Equivalent code: > > (arg) => { > if (typeof arg.key === 'undefined') { > arg.key = 1 > } > } > > if (typeof global === 'undefined') { > var global = {} > } > > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190112/39133157/attachment.html>
# Isiah Meadows (6 years ago)
I'd just prefer a way to detect the value undefined
vs no such binding.
I'd just prefer a way to detect the value `undefined` vs no such binding. On Sat, Jan 12, 2019 at 10:26 Sultan <thysultan at gmail.com> wrote: > >const [variable = defaultValue] = [maybeUndefinedValue] > > This would fail throw for variables you don't know exist i.e in the > following example polyfill will throw if Symbol doesn't exist. > > const [Symbol = SymbolPolyfill] = [Symbol] > > This is specifically why convention is to use: > > typeof maybeUndefinedValue === 'undefined' > > >const fn = ({ key = defaultValue }) => { console.log(key); } > > The second example doesn't assign the key prop to the passed object. > > > On Sat, Jan 12, 2019 at 5:54 PM Rob Ede <robjtede at icloud.com> wrote: > >> pretty succinct with existing de-structuring syntax: >> >> ``` >> const [variable = defaultValue] = [maybeUndefinedValue] >> >> const fn = ({ key = defaultValue }) => { console.log(key); } >> ``` >> >> On 11 Jan 2019, at 12:00, es-discuss-request at mozilla.org wrote: >> >> Send es-discuss mailing list submissions to >> es-discuss at mozilla.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> https://mail.mozilla.org/listinfo/es-discuss >> or, via email, send a message with subject or body 'help' to >> es-discuss-request at mozilla.org >> >> You can reach the person managing the list at >> es-discuss-owner at mozilla.org >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of es-discuss digest..." >> Today's Topics: >> >> 1. Syntax operator for "default assignment if value doesn't >> exits" (Sultan) >> >> *From: *Sultan <thysultan at gmail.com> >> *Subject: **Syntax operator for "default assignment if value doesn't >> exits"* >> *Date: *11 January 2019 at 10:52:12 GMT >> *To: *es-discuss <es-discuss at mozilla.org> >> >> >> >> An operator syntax for the the "typeof" pattern used to detect if a >> environment/object has a value: >> >> if (typeof variable === 'undefined') {...} >> if (typeof object.key === 'undefined') {...} >> >> This could manifest in destructuring as the following >> >> var fn = ({ key ||= 1 }) => { >> } >> >> And with variables as: >> >> var global ||= {} >> >> Equivalent code: >> >> (arg) => { >> if (typeof arg.key === 'undefined') { >> arg.key = 1 >> } >> } >> >> if (typeof global === 'undefined') { >> var global = {} >> } >> >> >> >> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> >> _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190112/b2a61ae8/attachment.html>
# Rob Ede (6 years ago)
Yeah I guess there exists a use case for that method still. But I don’t feel that use case merits special syntax.
Yeah I guess there exists a use case for that method still. But I don’t feel that use case merits special syntax. > On 12 Jan 2019, at 16:25, Isiah Meadows <isiahmeadows at gmail.com> wrote: > > I'd just prefer a way to detect the value `undefined` vs no such binding. >> On Sat, Jan 12, 2019 at 10:26 Sultan <thysultan at gmail.com> wrote: >> >const [variable = defaultValue] = [maybeUndefinedValue] >> >> This would fail throw for variables you don't know exist i.e in the following example polyfill will throw if Symbol doesn't exist. >> >> const [Symbol = SymbolPolyfill] = [Symbol] >> >> This is specifically why convention is to use: >> >> typeof maybeUndefinedValue === 'undefined' >> >> >const fn = ({ key = defaultValue }) => { console.log(key); } >> >> The second example doesn't assign the key prop to the passed object. >> >> >>> On Sat, Jan 12, 2019 at 5:54 PM Rob Ede <robjtede at icloud.com> wrote: >>> pretty succinct with existing de-structuring syntax: >>> >>> ``` >>> const [variable = defaultValue] = [maybeUndefinedValue] >>> >>> const fn = ({ key = defaultValue }) => { console.log(key); } >>> ``` >>> >>>> On 11 Jan 2019, at 12:00, es-discuss-request at mozilla.org wrote: >>>> >>>> Send es-discuss mailing list submissions to >>>> es-discuss at mozilla.org >>>> >>>> To subscribe or unsubscribe via the World Wide Web, visit >>>> https://mail.mozilla.org/listinfo/es-discuss >>>> or, via email, send a message with subject or body 'help' to >>>> es-discuss-request at mozilla.org >>>> >>>> You can reach the person managing the list at >>>> es-discuss-owner at mozilla.org >>>> >>>> When replying, please edit your Subject line so it is more specific >>>> than "Re: Contents of es-discuss digest..." >>>> Today's Topics: >>>> >>>> 1. Syntax operator for "default assignment if value doesn't >>>> exits" (Sultan) >>>> >>>> From: Sultan <thysultan at gmail.com> >>>> Subject: Syntax operator for "default assignment if value doesn't exits" >>>> Date: 11 January 2019 at 10:52:12 GMT >>>> To: es-discuss <es-discuss at mozilla.org> >> >>>> >>>> >>>> An operator syntax for the the "typeof" pattern used to detect if a environment/object has a value: >>>> >>>> if (typeof variable === 'undefined') {...} >>>> if (typeof object.key === 'undefined') {...} >>>> >>>> This could manifest in destructuring as the following >>>> >>>> var fn = ({ key ||= 1 }) => { >>>> } >>>> >>>> And with variables as: >>>> >>>> var global ||= {} >>>> >>>> Equivalent code: >>>> >>>> (arg) => { >>>> if (typeof arg.key === 'undefined') { >>>> arg.key = 1 >>>> } >>>> } >>>> >>>> if (typeof global === 'undefined') { >>>> var global = {} >>>> } >>>> >>>> >>>> >>>> >>>> _______________________________________________ >>>> es-discuss mailing list >>>> es-discuss at mozilla.org >>>> https://mail.mozilla.org/listinfo/es-discuss >>> >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190112/e0beac7e/attachment-0001.html>
An operator syntax for the the "typeof" pattern used to detect if a environment/object has a value:
if (typeof variable === 'undefined') {...} if (typeof object.key === 'undefined') {...}
This could manifest in destructuring as the following
var fn = ({ key ||= 1 }) => { }
And with variables as:
var global ||= {}
Equivalent code:
(arg) => { if (typeof arg.key === 'undefined') { arg.key = 1 } }
if (typeof global === 'undefined') { var global = {} }