Scope and Closures Flashcards

1
Q

What does nested scope look like?

A
function foo(a) {
	console.log( a + b );
}
var b = 2;
foo( 2 ); // 4

The RHS reference for b cannot be resolved inside the function foo, but it can be resolved in the Scope surrounding it (in this case, the global).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a += 2 represent?

A

a = a +2

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

How are variable constraints written?

A

EX: TAX_RATE

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

What is Lexical Scope

A

lexical scope is based on where variables and blocks of scope are authored, by you, at write time, and thus is (mostly) set in stone by the time the lexer processes your code.

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

Operator that displays the type of something

A

typeof

ex: a = ‘the’
typeof a // string

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

What is an array?

A

an object w/o properties/keys but indexed positions

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

Name all of the falsy values

A
" " empty string
0, -0 NaN (invalid numbers)
Null
Undefined
False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference? == and ===

A

== checks for value equality w/ coercion allowed
and
=== checks for value equality w/o allowing coercion

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

method to specify the number of decimal places and what does it return?

A
toFixed()
ex:
amount = 10.97777
amount.toFixed(2)
// '10.97'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When to use a switch statement?

A

Can be used instead of many if, else if statements

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

How to execute a function immediately

A

place function in ():

ex: (function (){
do something})();

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

What is closure

A
Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.
ex:
function foo() {
	var a = 2;
	function bar() {
		console.log( a );
	}
	return bar;
}
var baz = foo();
baz(); // 2 -- Whoa, closure was just observed, man.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Example of closure

A
function wait(message) {
	setTimeout( function timer(){
		console.log( message );
	}, 1000 );
}
wait( "Hello, closure!" );

We take an inner function (named timer) and pass it to setTimeout(..). But timer has a scope closure over the scope of wait(..), indeed keeping and using a reference to the variable message.

A thousand milliseconds after we have executed wait(..), and its inner scope should otherwise be long gone, that inner function timer still has closure over that scope.

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

What are modules

A

Most common used of closure. Modules let you define private variables/functions that are hidden from the outside world

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

What is Polyfilling

A

Taking the definition of a newer feature and producing a piece of code that equivalent to behavior but is able to run in an older js environment

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

What is Transpiling

A

A tool that converts newer code into older code equivalents

17
Q

What is state

A

ability to store values in variables and later retrieve or modify these values

18
Q

What is the difference between Reference error vs Type error

A

Ref is an error in scope

Type error is an error with the wrong value: null, undefied, etc.

19
Q

LHS/RHS

A

Left handed side lookup: the target of the assignment

Right handed side lookup: the source of the assignment

20
Q

Hoisting

A

Variable and function declarations are moved via the compiler from where they appear in code to the top.

assignments and executibles are left in place

21
Q

Two mechanisms that cheat lexical scope and why not to use them

A

eval(..)
with
The downside is that they defeat the enigne’s ability to perform compile-time optimizations regarding scope look-up. Code will run slower a result

22
Q

Anonymous functions vs named functions

A
function (){
do something}
anonymous func are quick/easy to type

difficult to debug
recursion is difficult
not as readable