Funtion Flashcards
(41 cards)
WHAT TYPE OF FUNCTION?
function isEven(num) { return num % 2 === 0; } isEven(24); // => true isEven(11); // => false
FUNCTION declaration
what are the 6 types of functions?
Function declaration Function expression Shorthand method definition Arrow function Generator function Function constructor
console.log(typeof hello); // => function hello(name) { return `Hello ${name}!`; }
‘function’
console.log(hello.name) // => function hello(name) { return `Hello ${name}!`; }
‘hello’
function hello(name) { return `Hello ${name}!`; } console.log(hello('Aliens')); // =>
‘Hello Aliens!’
WHAT TYPE OF FUNCTION?
function sum(a, b) { return a + b; } sum(5, 6); // => 11 ([3, 7]).reduce(sum) // => 10
A regular function
_______means that you declare the function once and later invoke it in many different places.
Regular function
the _________ in a statement always starts with the keyword function.
function declaration
WHAT TYPE OF FUNCTION?
var isTruthy = function(value) { return !!value; };
function expression
(function() { 'use strict'; if (true) { function ok() { return 'true ok'; } } else { function ok() { return 'false ok'; } } console.log(typeof ok === 'undefined'); // =>
true
(function() { 'use strict'; if (true) { function ok() { return 'true ok'; } } else { function ok() { return 'false ok'; } }
console.log(ok()); //
})();
Throws “ReferenceError: ok is not defined”
because the function declaration is inside a conditional block.
(function() { 'use strict'; var ok; if (true) { ok = function() { return 'true ok'; }; } else { ok = function() { return 'false ok'; }; } console.log(typeof ok === 'function'); // =>
})();
true
HOW TO FIX THIS?
(function() { 'use strict'; if (true) { function ok() { return 'true ok'; } } else { function ok() { return 'false ok'; } }
console.log(ok()); // Throws “ReferenceError: ok is not defined”
})();
make “function ok” into a function expression
The 3 things a function expression creates a function object that can be used in different situations:
- Assigned to a variable as an object count = function(…) {…}
- Create a method on an object sum: function() {…}
- Use the function as a callback .reduce(function(…) {…})
var getType = function(variable) { return typeof variable; }; getType.name // =>
empty string
var getType = function funName(variable) { console.log(typeof funName === 'function'); // => true return typeof variable; }
console.log(getType(3)); // =>
‘number’
var getType = function funName(variable) { console.log(typeof funName === 'function'); // => true return typeof variable; }
console.log(getType.name); // =>
‘funName’
var getType = function funName(variable) { console.log(typeof funName === 'function'); // => true return typeof variable;
console.log(typeof funName === ‘function’); // =>
false
____________ definition can be used in a method declaration on object literals and ES6 classes. You can define them using a function name, followed by a list of parameters in a pair of parenthesis (para1, …, paramN) and a pair of curly braces { … } that delimits the body statements.
Shorthand method
3 properties of arrow functions
- The arrow function does not create its own execution context, but takes it lexically
- The arrow function is anonymous: name is an empty string
- arguments object is not available in the arrow function
RETURNS AND WHY?
class Numbers { constructor(array) { this.array = array; } addNumber(number) { if (number !== undefined) { this.array.push(number); } return (number) function () { this.array.push(number); }; } } var numbersObject = new Numbers([]); numbersObject.addNumber(0); console.log(numbersObject.array)
error
function is NOT a arrow function
Without the arrow function is necessary to manually fix the context. It means adding fixes by using ______ method:
.bind()
ECMAScript 6 improves this usage by introducing the __________, which takes the context lexically. This is nice, because from now on is not necessary to use .bind(this) or store the context var self = this when a function needs the enclosing context.
arrow function
The __________ in JavaScript returns a_______ object. Its syntax is similar to function expression, function declaration or method declaration, just that it requires a star character *.
generator function
Generator