Shorthand Flashcards

1
Q

concise property initializers

A

function xyzzy(foo, bar, qux) {
return {
foo,
bar,
qux,
};
}

In each case, we merely use the name of the property we want to initialize, and JavaScript looks for a variable with the same name to use as the initial value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

concise methods

A

let obj = {
foo() {
// do something
},

bar(arg1, arg2) {
// do something else with arg1 and arg2
},
}
//The new concise method syntax lets you eliminate the : and the word function:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

object destructuring

A

let obj = {
foo: “foo”,
bar: “bar”,
qux: 42,
};

let foo = obj.foo;
let bar = obj.bar;
let qux = obj.qux;

//or
let { foo, bar, qux } = obj;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

renaming the variables in object destructuring

A

let { qux: myQux, foo, bar } = obj;
//This example creates a myQux variable that receives the value of obj.qux

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

destructuring with function parameters

A

function xyzzy({ foo, bar, qux }) {
console.log(qux); // 3
console.log(bar); // 2
console.log(foo); // 1
}

let obj = {
foo: 1,
bar: 2,
qux: 3,
};

xyzzy(obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

object destructuring as an assignment to existing variables

A

{ foo, bar, qux } = obj;
//will produce an error, { marks the beginning of a block rather than destructuring.
Instead:
({ foo, bar, qux } = obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

array destructuring

A

let foo = [1, 2, 3];
let [ first, second, third ] = foo;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

skipping elements in an array destructuring

A

let bar = [1, 2, 3, 4, 5, 6, 7];
let [ first, , , fourth, fifth, , seventh ] = bar;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

using array destructuring to perform multiple assignments in a single expression

A

let one = 1;
let two = 2;
let three = 3;

let [ num1, num2, num3 ] = [one, two, three];

console.log(num1); // 1
console.log(num2); // 2
console.log(num3); // 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how can we use spread syntax with the argument to a function

A

function add3(item1, item2, item3) {
return item1 + item2 + item3;
}

let foo = [3, 7, 11];

add3(…foo); // => 21

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

using spread to clone or concat arrays

A

// Create a clone of an array
let foo = [1, 2, 3];
let bar = […foo];
console.log(bar); // [1, 2, 3]
console.log(foo === bar); // false – bar is a new array

// Concatenate arrays
let foo = [1, 2, 3];
let bar = [4, 5, 6];
let qux = […foo, …bar];
qux; // => [1, 2, 3, 4, 5, 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does spread return with objects

A

Spread syntax with objects only returns the properties thatObject.keyswould return. That is, it only returns enumerable “own” properties. That means, in part, that it’s not the right choice when you need to duplicate objects that inherit from some other object. It also means that you lose the object prototype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

using spread to clone an object

A

// Create a clone of an object
let foo = { qux: 1, baz: 2 };
let bar = { …foo };
console.log(bar); // { qux: 1, baz: 2 }
console.log(foo === bar); // false – bar is a new object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does rest syntax do?

A

Rest syntax collects multiple items into an array or object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how do we use rest syntax with arrays?

A

//With Arrays
let foo = [1, 2, 3, 4];
let [ bar, …otherStuff ] = foo;
console.log(bar); // 1
console.log(otherStuff); // [2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how do we use rest syntax with objects?

A

//With Objects
let foo = {bar: 1, qux: 2, baz: 3, xyz: 4};
let { bar, baz, …otherStuff } = foo;
console.log(bar); // 1
console.log(baz); // 3
console.log(otherStuff); // {qux: 2, xyz: 4}

17
Q

where must a rest element be located?

A

The rest element (otherStuffin both examples) must be the last item in any expression that uses rest syntax.

18
Q

how do we use rest syntax with a function which takes an arbitrary number of parameters?

A

function maxItem(first, …moreArgs) {
let maximum = first;
moreArgs.forEach(value => {
if (value > maximum) {
maximum = value;
}
});

return maximum;
}

console.log(maxItem(2, 6, 10, 4, -3));

19
Q
A