0

I stumbled upon some strange JS feature(?) or behavior by accident and I cannot explain or google it.

function foo([a]) { alert(a); };
foo('Those brakets will make me use a string, but only uses the first char');

function foo({a}) { alert(typeof a); };
foo('This one will make "a" always be undefined');

And there might be more. Does this behavior have a name? Or can anyone explain it?

Maru
  • 894
  • 1
  • 12
  • 29
  • both snippets are not working. – Nina Scholz Feb 10 '16 at 14:38
  • What Browser are you using? The given codesnippets aren't working in my chrome, they just cause exceptions, because the browser isn't expection a "[" or a "{", but a parameter – Neutrosider Feb 10 '16 at 14:39
  • 3
    It's called destructuring, and is basically the same as https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Felix Kling Feb 10 '16 at 14:39
  • are you asking what is meaning of result of meaningless expression? – Daniel Feb 10 '16 at 14:39
  • Both are OK in ES2015, paste them into http://babeljs.io/repl/ and see what's the ES5 equivalent after transpilation. – pawel Feb 10 '16 at 14:44
  • @Neutrosider: I am using Chrome 48. They both work for me. – Maru Feb 10 '16 at 14:45
  • @FelixKling: Wow, thank you! That's interesting. If you make an answer, I can accept it :) – Maru Feb 10 '16 at 14:46
  • @Daniel: In my opinion, the two expressions are _not_ meaningless. They precisely show what I am troubled with. – Maru Feb 10 '16 at 14:46
  • @MarcoAlka sorry for my ignorance,i didn't know second one with {}, but is the first option with [] has description – Daniel Feb 10 '16 at 14:53

0 Answers0