ECMAScript 6 Flashcards
object = { ‘name’ : ‘Frank’, ‘age’ : 34 };
Show how to destructure the object into
var x, y;
( { name : x, age : y } = object );
What is the scoping of Var declared outside a function
Global
Object Destructuring
object = { ‘name’ : ‘Frank’, ‘age’ : 34 };
Show how to destructure the object into
var name, age;
[ name, age ] = object;
let b = 1; function () { let b = 2; }; What is b = ?
1
Show an example of a computed property name in an object.
object = {
[ “first” + “name”] : ‘Luther’
};
What does the ‘spread’ operator do?
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);
Show the ES6 way to define a function within an object.
let object = { myFunction() { console.log('hello world'); } } object.myFunction();
What about const objects: const boss = { 'firstname': 'Chuck', 'lastname': 'Duncan' }; ... boss.firstname = 'Pat';
Is this valid?
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.
Can a LET variable be reassigned?
as in Let a = 5; Let a = 10;
No it will throw an exception.
What is the scope of a var inside a function
Just the function contents
What is the scoping of Let
Block scoping
What is the scoping rule for ‘const’?
Block scoped
function myFn( { ‘name’ = ‘Chuck’, ‘age’ = 69, ‘profession’ = ‘genius’ } = { } ) { … }
myFn( { ‘name’ : ‘Pat’, ‘profession’ = ‘retired’ } );
Explain…
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.
What does the following produce:
var smallList = [ 'c', 'd', 'e' ]; var result = [ 'a', 'b', ...smallList, 'f', 'g' ]
result = [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ ]
Where is the ‘rest’ operator used?
In declaring a function; it embodies the ‘rest’ of the parameters that may have optionally been supplied:
function trafficTypes( lane, speed, …other );
[ a, b, c] = [ 1, 2, 3]
How can this be rewritten to NOT assign the ‘2’ to any variable.
[ a, , c] = [ 1, 2, 3]
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, b, c, d, e ] = myArray;
Likewise:
[ a, b, c, d, e ] = [ 1, 2, 3, 5, 8 ];
What delineates block scoping
{ }
var a = 1; Function () { var a = 2; } What is a =?
1, because the var in the function declares an scoped instance of a. Also, the anonymous function hasn’t been executed.
function sample(x=1, y=2, z=3) { ... Can a function parameter's value be an expression?
Yes
Given: let circleArea = ( pi, r ) => { return pi * r * r ; };
Restate this in the form of an anonymous function.
let circleArea = function(pi, r) { return pi * r * r; };
Can a var variable be reassigned
Yes
Name a difference between arrow functions and traditional functions.
The this operator works differently and an arrow function cannot be new-ed.
Object Destructuring
object = { ‘name’ : ‘Frank’, ‘age’ : 34 };
Show how to destruction object into
var name, age;
[name, age ] = object ;
In ES5: var x = 1, y = 2; var object = { x : x, y : y };
Rewrite this in ES6
let x = 1, y = 2; let object = { x, y };