Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

destruct a parameter and keep reference to it too

Is there a way in ES6 to destructure a parameter and reference it by name as well?

myfunction(myparam) {
    const {myprop} = myparam;
    ...
}

Can this be done in a single line in the function parameter list? Something similar to Haskell's @ in pattern matching.

like image 466
akonsu Avatar asked Jan 15 '16 16:01

akonsu


1 Answers

There is no syntax support for this. I guess you could hack around this with something like:

const myFunction = (function() {
  function myFunction(myparam, {myprop}) {
    // ...
  }

  return function(myparam) {
    return myFunction(myparam, myparam);
  };
}());

or even

function myFunction(myparam, {myprop}=myparam) {
  // ...
}

but both may be considered too hacky.

like image 149
Felix Kling Avatar answered Oct 29 '22 19:10

Felix Kling