Functions Flashcards
How do you define default parameter values?
function multiply(a, b = 1) { return a * b; } multiply(5, 1); // 5
What is a ‘rest’ parameter and how do you define it?
A function’s last parameter can be prefixed with three dots which will cause all the remaining user-supplied arguments to be placed with in an array.
function myFun(a, b, ...moreArgs) { console.log('a', a); console.log('b', b); console.log('moreArgs', moreArgs); }
myFun('one', 'two', 'three', 'four', 'five'); // a one // b two // moreArgs [ 'three', 'four', 'five' ]
What’s the difference between ‘rest’ parameters and the ‘spread’ operator?
Rest syntax looks like spread syntax, but in a way is opposite of spread. Spread expands an array into its elements, while rest collects multiple elements and condenses them into a single array.
How do you define an object property to a function that will be called when the property is looked up. For example, how to define the fahrenheit property so that it executes code to read/write to celsius internally?
const temp = new Temperature(22);
console. log(temp.fahrenheit); // 71.6
temp. fahrenheit = 86;
console. log(temp.celsius); // 30
Use getter/setters.
class Temperature { constructor(celsius) { this.celsius = celsius; }
get fahrenheit() { return this.celsius * 1.8 + 32; }
set fahrenheit() { this.celsius = (value - 32) / 1.8; } }
Name four ways to create a function.
(1) Function declaration
function isEven(num) { return num % 2 === 0; }
(2) Function expression
const count = function(x) { return x.length; }
(3) Shorthand method definition
const foo = { shortMethodDef(x) { console.log(x); }, traditionalMethodDef: function(x) { console.log(x); } }
(4) Arrow function
cons absValue = (number) => { if (number < 0) { return -number; } return number; }
Are function declarations hoisted?
Yes, function declarations are hoisted up to the top of the current scope, which means they can be invoked before the declaration.
hoisted(); // logs ‘bar’
function hoisted() { console.log('bar'); }
Are function expressions hoisted?
No, function expressions are not hoisted.
notHoisted(); // TypeError: notHoisted is not a function var notHoisted = function() { console.log('bar'); }
What is a method?
A method is a function associated with an object, or to put it another way, a method is a property of an object that is a function.
How do you define an arrow function?
An arrow function is defined with the list of parameters followed by a fat arrow followed by the function body.
const absValue = (number) => { if (number < 0) { return -number; } return number; }
Will ‘age’ get incremented here?
function Person() { this.age = 0;
setInterval(() => {
this.age++;
}, 1000);
}
Yes, because an arrow function does not have its own ‘this’. The ‘this’ value of the enclosing lexical scope is used.
Here, the ‘this’ properly refers to the Person object.
Will ‘age’ get incremented here?
function Person() { this.age = 0;
setInterval(function growUp() => {
this.age++;
}, 1000);
}
No, here ‘this’ refers to the global object so the growUp function won’t increment the age of Person.