1

I recently came across this nifty piece of JavaScript

and I am struggling a bit to understand how it works, and specifically this part :

  Array.from(e.attributes, ({name, value}) => [name, value])

Here, we deal with a NamedNodeMap which is a collection of Attr objects, and an Attr does have properties named name and value among many others. So, we have an anonymous function that takes an object and returns an array. So far, so good.

What I don't quite get is the way the argument of the function is defined as the litteral object {name, value}.

I was able to isolate the behavior :

> o={ a: 1, b: 2, ldfk: 'mùl' }
> let objToArr = function({a,b}){ return [a,b] }

> objToArr(o)
[ 1, 2 ]
> 
> o = {'dfklj':3.14, 'c':'z', 'a':1, 'foo':'bar', 'b':2, 'z':26 }
{ dfklj: 3.14, c: 'z', a: 1, foo: 'bar', b: 2, z: 26 }
> objToArr(o)
[ 1, 2 ]
> 

but I still don't understand why it works. Could someone please explain or refer me to the appropriate documentation ?

Éric Viala
  • 596
  • 2
  • 12
  • your first link does not work. – Nina Scholz Feb 03 '19 at 09:38
  • link fixe, thanks Nina – Éric Viala Feb 03 '19 at 09:40
  • It's called [Destructuring](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). Also: [Unpacking fields from objects passed as function parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Unpacking_fields_from_objects_passed_as_function_parameter) – adiga Feb 03 '19 at 09:40
  • Thanks adiga, that's the answer I was looking for. JavaScript never ceases to amaze me :-) – Éric Viala Feb 03 '19 at 09:45

3 Answers3

1

What you are looking for is a destructuring assignment, where an object is assigned to an object literal with only the keys, you need to take.

var object = { name_: 'foo', value: 42 },
    { name_, value } = object;           // name is a property of window

console.log(name_);
console.log(value);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Your not exactly defining an object literal as an argument, but rather you are destructuring the object. You can think of deconstruction as another way of accessing the properties of an object.

For example, if you have an object a:

const a = {
  name: 'foo',
  value: 'bar'
}

You can get its properties in a number of different ways:

Through bracket notation:

const name = a["name"];
const value = a["value"];

Via dot notation:

const name = a.name;
const value = a.value;

Or via destructuring assignment:

const {name, value} = a; // name & value are variables which you can use

const a = {
  name: "foo",
  value: "bar"
}

var name = a["name"];
var value = a["value"];
console.log(name, value);

var name = a.name;
var value = a.value;
console.log(name, value);

var {name, value} = a;
console.log(name, value);

Thus, when you use {name, value} in your function arguments, you are effectively telling javascript to extract the name and value properties from the object passed in as the argument.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

What I don't quite get is the way the argument of the function is defined as the litteral object {name, value}.

This is called destructuring assignment JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Hamza Ahmed
  • 521
  • 5
  • 12
  • 1
    No, it's not an "assignment" when used in the parameter declaration of a function. – Bergi Feb 03 '19 at 10:34
  • You can assign to new variables even if its used in the function. `const func = ({a: foo, b: bar}) => {console.log(foo, bar)}` [Link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names) – Hamza Ahmed Feb 04 '19 at 04:44