0

I'm coming from Python and made a JavaScript function like this:

var totalPrice = function({price: price, taxRate: taxRate}){
    return price + price * taxRate;
};

It works in Chrome, but I have three questions about it:

  1. What search terms should I have used to find out about this? I can't find this question asked elsewhere on SO, or anything about it in the MDN.
  2. Will this work in all browsers? Is it an ES2015 thing?
  3. Is this considered good or bad practice by any style guide you can reference? I'm also happy to hear gotchas, performance effects, or opinions.

Thanks!

Nathan Hinchey
  • 1,191
  • 9
  • 30
  • looks like a javascript hack :P – erwan Nov 10 '16 at 19:56
  • 1
    1) "Parameter destructuring" 2) No. Yes. 3) ["option objects"](http://stackoverflow.com/q/12826977/1048572) are usually only used with very many parameters. – Bergi Nov 10 '16 at 19:57
  • 1
    1. [Destructuring assignment](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) 2. It's an ES2015 thing 3. That depends on your style guide. But it's fairly common practise. N.b. you don't need the part to the right of the colon when the left and right are the same - you could use `{ price, taxRate }`. Think of the colon as "as". – joews Nov 10 '16 at 19:58
  • 1
    Comments should be answers. Except this one. – GolezTrol Nov 10 '16 at 19:59
  • 1
    @GolezTrol it's a dupe, but I figured a comment could help out the OP. – joews Nov 10 '16 at 20:47

0 Answers0