Adv Conc- Closures, private, IIFE, Shorthand Flashcards

1
Q

Is this a partial function?
function makeLogger(identifier) {
return function(msg) {
console.log(identifier + ‘ ‘ + msg);
};
}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is this a partial function?
function makeLogger(identifier) {
return function(msg) {
console.log(identifier, msg);
};
}

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a partial function

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s a closure?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is in a closure?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When is a closure created?

A

Closures are created when you define a function or method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the relationship between closures and scope?

A

the FUNCTION DEFINITION and all the IDENTIFIERS in its lexical scope become a single entity called a closure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do we mean when we say that closures are defined lexically?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When do closures use the lexical scope for a function to determine what variables that function can access.

A

at that function’s definition point

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain why private data is desirable. (3 things)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Make private data

A

function makeCounter() {
var count = 0; // declare a new variable
return function() {
count += 1; // references count from the outer scope
console.log(count);
};
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do parenthesis do?

A

tell JavaScript that we first want to evaluate that first as an expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens with this?
let x = function() {return 2}()
console.log(x);

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

(function() {
console.log(‘hello’);
}());

A

hello

the parenthesis basically make it an expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

((first, second) => first * second)(5, 6);

A

// => 30

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Rewrite this to use IIFE to hide the private data:
function makeUniqueIdGenerator() {
let count = 0;
return function() {
count += 1;
return count;
}
};

let makeUniqueId = makeUniqueIdGenerator();
makeUniqueId(); // => 1
makeUniqueId(); // => 2
makeUniqueId(); // => 3

A

const makeUniqueId = (function() {
let count = 0;
return function() {
++count;
return count;
};
})();

makeUniqueId(); // => 1
makeUniqueId(); // => 2
makeUniqueId(); // => 3

17
Q

help prevent not knowing what variables are already in use for large files by not declaring a function. (2ways)

A

IIEF

blocks
{
}

18
Q

Do concise syntax for Property Initializers for an object

A

function xyzzy(foo, bar, qux) {
return {
foo,
bar,
qux,
};
}

19
Q

Do concise syntax for setting methods

A

let obj = {
foo() {
// do something
},

bar(arg1, arg2) {
// do something else with arg1 and arg2
},
}

20
Q

destructure an object’s properties into individual variables

Try omitting some names

A

let obj = {
foo: “foo”,
bar: “bar”,
qux: 42,
};

let { foo, bar, qux } = obj;
OR
let { foo } = obj;
let { bar, qux } = obj;

21
Q

destructure an object’s properties into individual variables and use different names

A

let { qux: newQux} = obj;

22
Q

destructure an object’s properties into individual function arguments

A

let obj = {
foo: 1,
bar: 2,
qux: 3,
};

function xyzzy({ foo: newFoo, bar, qux }) {
console.log(qux); // 3
console.log(bar); // 2
console.log(newFoo); // 1
}

xyzzy(obj);

23
Q

Destructure an object to reassign variables (as opposed to initialize them)

A

This does not work:
{ foo, bar, qux } = obj;
Nor does this
({ foo, bar, qux }) = obj;
So do this:
({ foo, bar, qux } = obj);

24
Q

destructure an array’s properties into individual variables

A

let foo = [1, 2, 3];
let [ first, second, third ] = foo;

25
Q

destructure an array’s properties into individual variables but skip some elements

A

let bar = [1, 2, 3, 4, 5, 6, 7];
let [ first, , , fourth, fifth, , seventh ] = bar;

26
Q

What’s good form for destructuring arrays?

A

The spaces inside the brackets aren’t required. However, it’s easier to read and more clearly distinguishes between destructuring and array literals.

let foo = [1, 2, 3];
let [ first, second, third ] = foo;

27
Q

Swap the values of variables using array destructuring

A

let one = 1;
let two = 2;

[ one, two ] = [two, one];

console.log(one); // 2
console.log(two); // 1

28
Q

What will this do?

let foo = [1, 2, 3, 4];
let [ bar, …qux ] = foo;
console.log(bar);
console.log(qux);

A

// 1
// [2, 3, 4]

29
Q

What does this do?
add3(…foo);

A

“spread” the elements of an array or object into separate items.

30
Q

let foo = [1, 2, 3];
let bar = […foo];
console.log(bar);
console.log(foo === bar);

A

// [1, 2, 3]
// false – bar is a new array

31
Q

let foo = [1, 2, 3];
let bar = [4, 5, 6];
let qux = […foo, …bar];
qux;

A

// => [1, 2, 3, 4, 5, 6]

32
Q

let foo = [1, 2, 3]
let bar = […foo, 4, 5, 6, …foo];
bar;

A

// => [1, 2, 3, 4, 5, 6, 1, 2, 3]

33
Q

let foo = { qux: 1, baz: 2 };
let bar = { …foo };
console.log(bar);
console.log(foo === bar);

A

// { qux: 1, baz: 2 }
// false – bar is a new object

34
Q

Spread syntax returns only enumeralbe properties of an object
t or f

A

True
Spread syntax with objects only returns the properties that Object.keys would return. That is, it only returns enumerable “own” properties. That means, in part, that it’s not the right choice when you need to duplicate objects that inherit from some other object. It also means that you lose the object prototype.

For now, all you need to know about enumerable properties is that the object prototype is not enumerable. Also, the length property of an array is not enumerable.

35
Q

What does this do?
let foo = [1, 2, 3, 4];
let [ bar, …otherStuff ] = foo;
console.log(bar);
console.log(otherStuff);

A

// 1
// [2, 3, 4]

36
Q

let foo = {bar: 1, qux: 2, baz: 3, xyz: 4};
let { bar, baz, …otherStuff } = foo;
console.log(bar);
console.log(baz);
console.log(otherStuff);

A

// 1
// 3
// {qux: 2, xyz: 4}

37
Q

let array1 = [1, 2, 3, 4]

let [one, …two, three] = array1;

console.log(two);

A

Syntax error

38
Q

function maxItem() {
let maximum = arguments[0];
[].forEach.call(arguments, value => {
if (value > maximum) {
maximum = value;
}
});

return maximum;
}

console.log(maxItem(2, 6, 10, 4, -3));

A

10

39
Q

rewrite

function maxItem() {
let maximum = arguments[0];
[].forEach.call(arguments, value => {
if (value > maximum) {
maximum = value;
}
});

return maximum;
}

console.log(maxItem(2, 6, 10, 4, -3));

using the rest syntax

A

function maxItem(first, …moreArgs) {
let maximum = first;
moreArgs.forEach(value => {
if (value > maximum) {
maximum = value;
}
});

return maximum;
}

console.log(maxItem(2, 6, 10, 4, -3));