Adv Conc- Closures, private, IIFE, Shorthand Flashcards
Is this a partial function?
function makeLogger(identifier) {
return function(msg) {
console.log(identifier + ‘ ‘ + msg);
};
}
Here, console.log takes exactly one argument and the function returned by makeLogger also takes exactly one argument. Since there is no difference in the number of arguments, we don’t have partial function application.
Is this a partial function?
function makeLogger(identifier) {
return function(msg) {
console.log(identifier, msg);
};
}
What is a partial function
A function such as makeAdder is said to use partial function application. It applies some a function’s arguments (the add function’s first argument here) when called, and applies the remaining arguments when you call the returned function. Partial function application refers to the creation of a function that can call a second function with fewer arguments than the second function expects. The created function applies the remaining arguments.
function add(first, second) {
return first + second;
}
function makeAdder(firstNumber) {
return function(secondNumber) {
return add(firstNumber, secondNumber);
};
}
let addFive = makeAdder(5);
let addTen = makeAdder(10);
console.log(addFive(3)); // 8
console.log(addFive(55)); // 60
console.log(addTen(3)); // 13
console.log(addTen(55)); // 65
What’s a closure?
You can think of closure as a function combined with any variables from its lexical scope that the function needs.
Closures are created when you define a function or method. The closure essentially closes over its environment – what’s in lexical scope. In effect, the function definition and all the identifiers in its lexical scope become a single entity called a closure.
When the function is invoked, it can access any variables it needs from that environment. That is, the function can use variables from the lexical scope where the function was defined. Even if those variables aren’t in the lexical scope where you invoke the function, it can still access them.
What is in a closure?
The closure essentially closes over its environment – what’s in lexical scope. In effect, the FUNCTION DEFINITION and all the IDENTIFIERS in its lexical scope become a single entity called a closure.
When is a closure created?
Closures are created when you define a function or method.
What is the relationship between closures and scope?
the FUNCTION DEFINITION and all the IDENTIFIERS in its lexical scope become a single entity called a closure.
What do we mean when we say that closures are defined lexically?
A closure includes the variables it needs from the scope where you defined the function. Those variables may not be in scope when you invoke the function, but they’re still available to the function.
let apple = function() {
let orange = 1;
return function() {
return orange;
}
}
let purple = apple();
console.log(purple()); // 1
console.log(orange); //error
When do closures use the lexical scope for a function to determine what variables that function can access.
at that function’s definition point
Explain why private data is desirable. (3 things)
excellent way to force other developers to use the intended interface. By keeping the collections private, we enforce access via the provided methods.
Maybe the code forces you to use add to add an item to the list, which ensures that we can log every addition.
prevent the user of an object from becoming dependent on the implementation. Other developers shouldn’t care that a todo-list object stores todos in an array, and your code shouldn’t encourage them to depend on it. Instead, your object should supply an API that lets other developers manipulate the todo-list regardless of its implementation. If you later change the implementation, your API should remain the same.
Make private data
function makeCounter() {
var count = 0; // declare a new variable
return function() {
count += 1; // references count from the outer scope
console.log(count);
};
}
What do parenthesis do?
tell JavaScript that we first want to evaluate that first as an expression
What happens with this?
let x = function() {return 2}()
console.log(x);
2
(function() {
console.log(‘hello’);
}());
hello
the parenthesis basically make it an expression.
((first, second) => first * second)(5, 6);
// => 30