ECMAScript 6 Flashcards

1
Q

object = { ‘name’ : ‘Frank’, ‘age’ : 34 };

Show how to destructure the object into
var x, y;

A

( { name : x, age : y } = object );

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

What is the scoping of Var declared outside a function

A

Global

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

Object Destructuring
object = { ‘name’ : ‘Frank’, ‘age’ : 34 };

Show how to destructure the object into
var name, age;

A

[ name, age ] = object;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
let b = 1;
function () {
    let b = 2;
};
   What is b = ?
A

1

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

Show an example of a computed property name in an object.

A

object = {
[ “first” + “name”] : ‘Luther’
};

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

What does the ‘spread’ operator do?

A
It takes and array (list) and 'spreads' out the contents as if they were individual values applied separately.
function threeParms(a, b, c) ...
as in:
var list = [ 10, 20, 26 ];
threeParam(...list);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show the ES6 way to define a function within an object.

A
let object = {
       myFunction() {
           console.log('hello world');
           } 
      }
object.myFunction();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What about const objects:
const boss = { 'firstname': 'Chuck', 'lastname': 'Duncan' };
...
boss.firstname = 'Pat';

Is this valid?

A

Yes, because the constant has an object assigned to it and that hasn’t changed. What changed was the contents of the object. That is legal.

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

Can a LET variable be reassigned?

as in Let a = 5; Let a = 10;

A

No it will throw an exception.

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

What is the scope of a var inside a function

A

Just the function contents

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

What is the scoping of Let

A

Block scoping

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

What is the scoping rule for ‘const’?

A

Block scoped

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

function myFn( { ‘name’ = ‘Chuck’, ‘age’ = 69, ‘profession’ = ‘genius’ } = { } ) { … }

myFn( { ‘name’ : ‘Pat’, ‘profession’ = ‘retired’ } );

Explain…

A

The myFn arguments effectively replace the { } and the assignment continues. If NO parameter’s are given, the { } is applied, but the parameters each have default values, which are assigned.

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

What does the following produce:

var smallList = [ 'c', 'd', 'e' ];
var result = [ 'a', 'b', ...smallList, 'f', 'g' ]
A

result = [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ ]

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

Where is the ‘rest’ operator used?

A

In declaring a function; it embodies the ‘rest’ of the parameters that may have optionally been supplied:

function trafficTypes( lane, speed, …other );

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

[ a, b, c] = [ 1, 2, 3]

How can this be rewritten to NOT assign the ‘2’ to any variable.

A

[ a, , c] = [ 1, 2, 3]

17
Q

Given myArray = [ 1, 2, 3, 5, 8 ];
And var= a, b, c, d, e;

Show how to “destructure” the values in the myArray into the vars.

A

[ a, b, c, d, e ] = myArray;

Likewise:
[ a, b, c, d, e ] = [ 1, 2, 3, 5, 8 ];

18
Q

What delineates block scoping

A

{ }

19
Q
var a = 1;
Function () {
   var a = 2;
}
    What is a =?
A

1, because the var in the function declares an scoped instance of a. Also, the anonymous function hasn’t been executed.

20
Q
function sample(x=1, y=2, z=3) {
...
Can a function parameter's value be an expression?
A

Yes

21
Q
Given:
   let circleArea = ( pi, r ) => { return pi * r * r ; };

Restate this in the form of an anonymous function.

A

let circleArea = function(pi, r) { return pi * r * r; };

22
Q

Can a var variable be reassigned

A

Yes

23
Q

Name a difference between arrow functions and traditional functions.

A

The this operator works differently and an arrow function cannot be new-ed.

24
Q

Object Destructuring

object = { ‘name’ : ‘Frank’, ‘age’ : 34 };

Show how to destruction object into
var name, age;

A

[name, age ] = object ;

25
Q
In ES5:
var x = 1, y = 2;
var object = { x : x, y : y };

Rewrite this in ES6

A
let x = 1, y = 2;
let object = { x, y };