Funtion Flashcards
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
WHAT TYPE OF FUNCTION?
function* indexGenerator(){ var index = 0; while(true) { yield index++; } }
Generator
x=>x*2
write as a regular function declaration
function (x) { return x * 2; }
what scope is “VAR”?
function scope
what scope is “LET”
block scope
function sum1(a, b) { return a + b; } var sum2 = function(a, b) { return a + b; } var sum3 = (a, b) => a + b; console.log(typeof sum1 === 'function'); // =>
true
function sum1(a, b) { return a + b; } var sum2 = function(a, b) { return a + b; } var sum3 = (a, b) => a + b; console.log(typeof sum3 === 'function'); // =>
true
function setupWhatever (){ console.log(x) var x = 3; console.log(x); }
setupWhatever();
//returns?
undefined
3
function setupWhatever (){ console.log(x) let x = 3; console.log(x); }
setupWhatever();
//returns?
syntax error.
let cannot be defined after
function setupWhatever (){
let x; console.log(x); x = 3; console.log(x) }
setupWhatever();
undefined
3
what is block scope
anything in between curly brackets, if, for….
function setupWhatever (){
for (var x = 1; x < 100; x++){
}
console.log(x);
}
setupWhatever();
100
function setupWhatever (){
for (let x = 1; x < 100; x++){
}
console.log(x);
}
setupWhatever();
error.
One possible application of _______ is a better way to access the global object in a browser or NodeJS script:
new Function
The functions created this way don’t have access to current scope, thus closures cannot be created. They are always created in the global scope.
new functions
If the function should use this from the enclosing function, the________ function is a good solution. When the callback function has one short statement, the function is a good option too, because it creates short and light code.
arrow
________ way to declare functions generally should not be used. Mainly because it opens potential security risks, doesn’t allow code auto-complete in editors and lose the engine optimizations
new Function
with JS a outside variable is or is not available inside a function
is