Advanced JS Flashcards

1
Q

What type of scoping rule(s) does Javascript have?

A

Lexical

one floor up in scope until you hit the global vs. dynamic - call stack

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

What exceptions or cheats are used with the kind of scoping rule(s) JavaScript has?

A

eval and wait

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

What are the different ways you can create a new scope?

A

Functions, catch block, (block scope) {} with let

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

What’s the difference between undeclared and undefined?

A

Undefined was declared but never initialized, undeclared was never declared, doesn’t exist in any scope. Undefined is a value, but a variable that is undefined does not currently have a value.

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

What determines which object a function’s this points to? What’s the default?

In order of precedence

A
  1. new keyword
  2. Explicit Binding: .call() or .apply() / hard binding
  3. Implicit Binding: Owning or containing object (obj.function call)
  4. Default: global unless strict mode is on and then it is undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you “borrow” a function by implicit assignment of this?

A

When you call the function in the context of an object - mutate the object to put a reference of the function on that object.

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

How do you explicitly bind this?

A

.call() or .apply()

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

How can you seal a specific object to a function? Why do that? Why not?

A

.bind() - to be predictable for the this binding, but you loose flexibility.

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

How do you create a new this object?

A

By using the new keyword

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

What is a closure and how is it created?

A

Function that remembers and accesses its lexical scope even when it is executed outside of its lexical scope. It is created when an inner function is transported out of the outer function.

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

How long does its scope stay around?

A

As long as there is some function that has a closure to a scope it will stay, but as soon as they all go away the scope will go away.

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

Why doesn’t a function callback inside a loop behave as expected? How do we fix it?

A

Because it shares a scope since the variable is not created per iteration so when the value changes it will give whatever value is present at the time of execution. We fix it by wrapping it in an IIFE or use let.

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

How do you use a closure to create an encapsulated module? What are the benefits of that approach?

A

Has to have an outer-wrapping function and return one or more inner functions that have a closure over the scope. Benefit: public API, encapsulation, least privilage.

Tradeoff - modules hide/make inner functions hard or impossible to test.

Tradeoff: New modules creates a whole bunch of extra copies

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

What is a constructor?

A

A function called with the new keyword in front of it.

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

What is [[Prototype]] and where does it come from?

A

Linkage from one object to another object - can get from Object.create or from the new keyword.

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

How does a [[Prototype]] affect an object?

A

Similar to lexical scope it provides the prototype chain and allows traversal (delegates) for function calls and properties

17
Q

How do we find out where an object’s [[Prototype]] points to (3 ways)?

A

.\_\_proto\_\_, Object.getPrototypeOf(), .constructor.prototype

18
Q

What does the following this reference to for the a1.speak call?

function Foo(who) {
this.me = who;
}

Foo.prototype.speak = function() {
alert("Hello, I am " + this.me + ".");
};

var a1 = new Foo("a1");

$("#speak").click(a1.speak);
A

this will point to the button instead of the a1 object because of the call.

19
Q

How is Javascript’s [[Prototype]] chain not like traditional/classical inheritance?

A

Javascript doesn’t copy down for class heirarchy instead it links or delegates up. The arrow is going the opposite direction.

20
Q

What does “behavior delegation” mean and how does it describe object linking in JS?

A

Behavior delegation is the idea that instead of copying and having no link to the class heirarchy the objects delegate behavior to one another through prototype chain link traversal.

21
Q

Why is “behavior delegation” as a design pattern a helpful thing? What are the tradeoffs?

A

You don’t have copies of methods. With delegation we are embracing the fact that all objects continue to exist and they are dynamically and changing. Classes are snapshot copies so any change to the parent doesn’t make any changes to the child. You can implement classes in delegation but not the other addition.

Tradeoff: shadowing is awkward. Everything is public so encapsulation is not possible.

22
Q

What is “callback hell”? Why do callbacks suffer from “inversion of control”?

A

Callback hell is giving over control of your program to some other utility and all the trust that invovles.

23
Q

How do you pause a generator?

A

yield

24
Q

How do you resume a generator?

A

next()

25
Q

What is a promise? How does it solve inversion of control issues?

A

A promise is something that will eventually either be rejected or accepted - a promise of a future value. It solves inversion of control because you receive a promise back not a specific value or passing in my continuation.

26
Q

How do we combine generators and promises for flow control?

A

The generator is yielding out promises and when the promise completes it automatically restarts the generator. Generator returns a promise and when the promise is complete have the promise restart the generator.