ES6 Flashcards
Master the language
Arrow functions props
- Cannot be used as constructors;
- They have no “this”;
- They have no “super”;
- They have no new.target;
- They have no prototype;
- They have no “arguments” object;
- They have no duplicate named parameters.
What do arrow functions of 1 parameter return?
The expression to the right
In what do you have to wrap an arrow function that returns an object literal?
In ( ); Example: let myResult = id => ({id: id, name: "RSE});
Can you change “this” by using bind(), call() or apply() in arrow functions?
No
Can you access in arrow function the “arguments” from a function that contains it?
Yes. Ex: function containingFunction(){ return () => argument[0]; }
var myFunction = containingFunction(5); console.log(myFunction()); // 5
Can you use bind(), call() and apply() on arrow functions?
yes
What is the difference between the new string methods includes() and startsWith() on one side, and endsWidth() on the other side? Clue: something related to 2nd parameter
endsWidth() starts the match from the length of the string minus the 2nd argument, whereas startsWith() and includes() start the match from that index given by the 2nd arg
Having a function: function makeRequest(a,b=3,c){ return a+b+c; }
what is the function returning if I do makeRequest(2,null,5)?
a. null;
b. 10;
c. 7;
d. 192
c) because if I use null for a default parameter, null is considered valid and that value will NOT be used.
How is the behaviour of “arguments” different in ES5 vs ES6?
In ES5 non-strict mode, “arguments” will “follow” a function named arguments;
In ES5 strict mode, “arguments” does not reflect the changes of named parameters;
In ES6, “arguments” is detached from named arguments, irrespective of strict or non-strict node.
If I have a function:
function mixArgs(first,second=”b”){ console.log(arguments)}
- What is the value of arguments[1] if I call the function as in mixArgs(100)?
- Also what is the function call f=mixArgs(100) going to print?
- undefined, because arguments[1] = undefined
2. Arguments [100, calee(…), Symbol(Symbol.iterator):f]
Why is possible and correct to write:
function getValue(value){ return value+5};
function add(first, second = getValue(first)){ return first + second;}
Because in ES6 it is allowed to use a previous parameter as a default for a second parameter.
(not allowed the other way around though)
In loops, in order to avoid creating closures, what is the recommended concept to use instead of plain functions?
IIFEs;
What is Javascript doing when it looks through an upcoming block and finds a variable declaration?
What is the difference behaviour between “var” compared to “let” and “const” declarations in this case?
The declaration with “var” is hoisted at the beginning of the current scope;
The “const” and “let” declarations are placed in the TDZ, where they will stay until that execution flow passes the declaration point.
“let” and “const” are NOT hoisted!!
Which ones of the two typeof instructions below will return “undefined” and which one will throw an error?
console.log(typeof(a)); let f3 = function() { console.log(typeof(a)); let a = 3; }
The 2nd typeof will throw an error, because a is in the TDZ at the moment of the call of that typeof, whereas the first typeof will just return “undefined”, because the variable a at that scope is not in the TDZ (is no blocked by a “let” or a “const” declaration at that scope level)
When is the parameter initialization happening for a function?
a. When the function is declared;
b. When the function is called;
When the function is called
Function parameters and function body have:
a. the same scope or a different scope;
b. the same TDZ or a different TDZ.
Both are different»_space;> a function’s default params cannot access any variables declared inside the function body
What is the “pick()” function in the “Underscore.js” library doing?
Returns a copy of a given object with some specific subset of the original properties.
What is the function presented below going to return if called as: let f = function(x,...y){ console.log(arguments.length)} let a = 100; let b = [200, 300]; f(a,b);
3
What is the console.log() presented below going to print?
let f = function(x,...y){ x+y[0]} let a = 100; let b = [200, 300]; console.log(f.length);
1, and not 2 or 3, because rest parameters DO NOT affect the function’s length property
What are the rest parameters restrictions?
- There can only be one rest parameter;
- The rest parameter has to be the last of the function parameters;
- The rest parameters cannot be used in an object literal setter, like:
let object = { // Syntax error set name(...value){ // do something } };
How are the rest parameters influencing the “arguments” object for a function?
There is no influence, “arguments” will reflect 3 if f(a,…b) and a=100,b=200,c=300.
What is the spread operator used usually to replace?
The apply() method as it is far easier to do
Math.max(…values, 0)) where
values=[100,200];
What is the restriction on the object literal setters?
They are restricted to a single argument
What are the differences between spread parameters and rest arguments?
1. Spread parameters are used when you call the function: var abc = ['a', 'b', 'c']; var def = ['d', 'e', 'f']; var alpha = [ ...abc, ...def];
Rest arguments are used when you define a function: function sum(first, ...others){.........};
- Rest argument has to be only one, whereas the spread parameters can be as many as you like;
- The rest argument has to come on the last position, whereas the spread arguments can have any position.