JavaScript Arrow Functions
Advanced-JS Functions: Arrow Functions
What is an arrow function?
View Answer:
// Traditional Function
function (a){
return a + 100;
}
// Arrow Function Break Down
// 1. Remove the word "function" and place arrow between
// the argument and opening body bracket
const arrowFunc = (a) => {
return a + 100;
}
// 2. Remove the body brackets and word "return" -- the return is implied.
const arrowFunc = (a) => a + 100;
// 3. Remove the argument parentheses
const arrowFunc = a => a + 100;
console.log(arrowFunc(200)); // logs 300
What is the meaning of Arrow functions having no "this"?
View Answer:
let obj = {
a: 'object???',
// Arrow Function
foo: () => {
console.log(this.a);
}, // this.a is referencing the global this
};
let a = 'global!!!'; // global this
obj.foo(); // returns undefined
///////////////////////////////////
let obj2 = {
b: 'object???',
// Property Function
foo: function () {
console.log(this.b);
}, // this.a is referencing the global this
};
let b = 'global!!!'; // global this
Why can’t you use the new.target keyword with Arrow Functions?
View Answer:
let X = () => {};
let Y = function () {};
// program stops here: uncaught type error
x = new X(); // X is not a constructor
y = new Y(); // Y does not execute
This is relatively simple, as seen by the engine's reaction to any call of "new" on arrow functions. Because "blank is not a constructor," it results in an uncaught type error.
What distinguishes arrow function from regular functions? In terms of how they bind the "this" value?
View Answer:
Let's consider an example where the behavior of 'this' differs between a regular function and an arrow function.
let obj = {
value: 'Hello',
regularFunction: function() {
console.log(this.value); // refers to obj
},
arrowFunction: () => {
console.log(this.value); // refers to surrounding scope, not obj
}
};
obj.regularFunction(); // outputs: 'Hello'
obj.arrowFunction(); // likely outputs: undefined
In the regularFunction method, this refers to obj as expected. But in arrowFunction, this does not refer to obj. It refers to the enclosing lexical context, which is the global scope in this case. If value is not defined in the global scope, this will output undefined.
Can you explain one benefit of Arrow functions having no arguments variable?
View Answer:
function defer(f, ms) {
return function () {
setTimeout(() => f.apply(this, arguments), ms);
};
}
function sayHi(who) {
console.log('Hello, ' + who);
}
let sayHiDeferred = defer(sayHi, 2000);
sayHiDeferred('John'); // Hello, John after 2 seconds
What is a good use case for Arrow Functions?
View Answer:
Here's a simple example of using an arrow function as a callback function in JavaScript.
let numbers = [1, 2, 3, 4, 5];
// Here, an arrow function is used as the callback function for the .map() method.
let squares = numbers.map((number) => number * number);
console.log(squares); // outputs: [1, 4, 9, 16, 25]
Can arrow functions be used in decorators?
View Answer:
What is the definition of a Higher-Order function?
View Answer:
// Higher Order Function
function greaterThan(n) {
return (m) => m > n;
}
let greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// → true
// We can have functions that change other functions.
function noisy(f) {
return (...args) => {
console.log('calling with', args);
let result = f(...args);
console.log('called with', args, ', returned', result);
return result;
};
}
noisy(Math.min)(3, 2, 1);
// → calling with [3, 2, 1]
// → called with [3, 2, 1] , returned 1
// We can even write functions that provide new types of control flow.
function unless(test, then) {
if (!test) then();
}
repeat(3, (n) => {
unless(n % 2 == 1, () => {
console.log(n, 'is even');
});
});
// → 0 is even
// → 2 is even
Functions that operate on other functions are called higher-order functions, either by taking them as arguments or returning them.