destructure an argument, and retain a reference to the original argument

better shown with an example:

  ;(({lala, another} = obj) => {
    console.log('obj.lala = ', lala);
    console.log('obj.another = ', another);
    console.log('obj = ', obj);
  })({lala: 1234, another: 'one'});

it's already valid syntax, however, it will spit out an error saying that 'obj' is undefined.

this simplifies greatly the pattern of passing around a context from function to function. for example:

const render_something = (ctx) => {
  let { h } = ctx
  return h('div',
    // ...
    render_something_else(ctx)
  )
}

when this could be simplified to just:

const render_something = ({ h } = ctx) => {
  return h('div',
    // ...
    render_something_else(ctx)
  )
}

after posting this, I realised that the reason why it's valid syntax is because it is considering obj to be the deafult argument. I would argue that defaults for destructured arguments doesn't conceptually make a whole lot of sense, but it seems to work...

const outer_obj = { lala: 'lala' }
const destructure_default = ({lala} = outer_obj) => {
  console.log('lala = ', lala)
}

destructure_default()
// > lala = lala

so, then because this is the case, perhaps I may suggest the syntax:

const fn = ({h}:ctx, [first]:arr) => {}

Have you seen https://github.com/zkat/proposal-as-patterns?

1 Like

no, I hadn't seen that. it looks really great, actually! so the syntax, using her proposal would be:

const fn = ({h} as ctx, [first] as arr) => {
  // ...
}

however her proposal does not show off what I suggested, only the assignment of the inner match to a variable (inside the destructured argument). I looked briefly through the issues to see if that's mentioned, and it looks like there is one.

I'll forward my idea on to there. thanks for sharing this proposal