183 questions
183
votes
14
answers
82k
views
One-liner to take some properties from object in ES 6
How one can write a function, which takes only few attributes in most-compact way in ES6?
I've came up with solution using destructuring + simplified object literal, but I don't like that list of ...
20
votes
3
answers
7k
views
What is destructuring assignment and its uses?
I have been reading about Destructuring assignment introduced in ES6.
What is the purpose of this syntax, why was it introduced, and what are some examples of how it might be used in practice?
12
votes
1
answer
2k
views
Where can I get info on the object parameter syntax for JavaScript functions?
If I want to call a function like this:
moo({ a: 4 });
Normally I'd have to phrase my function definition like this:
function moo(myArgObj) {
print(myArgObj.a);
}
But this awesome syntax is ...
36
votes
4
answers
15k
views
What do square brackets mean on the left-hand side of variable assignment (`[ (…) ] = (…)`)?
For a project, a developer sent us a JS file with code similar to this:
var myList = [ 1, 2, 3 ];
var a, b, c;
[ a, b, c ] = myList;
It works in Opera 10.30, and Firefox 3.6.x, but it’s not okay for ...
29
votes
5
answers
6k
views
ES6 destructuring function parameter - naming root object
Is there a way to retain the name of a destructured function argument? I.e., the name of the root object?
In ES5, I might do this (using inheritance as a metaphor to make the point):
// ES5:
var ...
191
votes
17
answers
102k
views
Is it possible to destructure onto an existing object? (JavaScript ES6)
For example, if I have two objects:
var foo = {
x: "bar",
y: "baz"
}
and
var oof = {}
and I wanted to transfer the x and y values from foo to oof. Is there a way to do that ...
13
votes
4
answers
1k
views
ES6 Array destructuring weirdness
Can anyone explain, why the following happens with ES6 array destructuring?
let a, b, c
[a, b] = ['A', 'B']
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)
Expected: a=A b=BB c=C
...
151
votes
4
answers
34k
views
What does curly brackets in the `var { ... } = ...` statements do?
Not sure if this is a Mozilla-specific JS syntax, but I often found variables being declared this way, for example, in add-on SDK docs:
var { Hotkey } = require("sdk/hotkeys");
and in various chrome ...
52
votes
5
answers
17k
views
How do I destructure all properties into the current scope/closure in ES2015?
I'd like to do something like this:
const vegetableColors = {corn: 'yellow', peas: 'green'};
const {*} = vegetableColors;
console.log(corn);// yellow
console.log(peas);// green
I can't seem to find ...
36
votes
1
answer
13k
views
ES6/ES2015 object destructuring and changing target variable
How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
...
99
votes
3
answers
34k
views
How can I destructure object properties with key names that are invalid variable names?
As object keys are strings, they can contain any kind of characters and special characters. I recently stumbled upon an object which I receive from an API call. This object has '-' in it's key names.
...
374
votes
8
answers
242k
views
Types in object destructuring
This
const { foo: IFoo[] } = bar;
and this
const { foo: Array<IFoo> } = bar;
will reasonably cause an error.
And this
const { foo: TFoo } = bar;
will just destructure TFoo property.
How can ...
187
votes
3
answers
122k
views
Destructuring and rename property
const a = {
b: {
c: 'Hi!'
}
};
const { b: { c } } = a;
Is it possible rename b in this case? I want get c and also rename b.
172
votes
19
answers
171k
views
Destructuring to get the last element of an array in ES6
In CoffeeScript, this is straightforward:
coffee> a = ['a', 'b', 'program']
[ 'a', 'b', 'program' ]
coffee> [_..., b] = a
[ 'a', 'b', 'program' ]
coffee> b
'program'
Does ES6 allow for ...
78
votes
3
answers
46k
views
Destructuring in Node.JS
This recent video claims that EMCAScript 6 destructuring is already partially implemented in Node.JS. I have tried various examples (using v0.10.12 and the --harmony flag), such as
var [a, b] = [1, 2]...