Advanced JS Flashcards
What type of scoping rule(s) does Javascript have?
Lexical
one floor up in scope until you hit the global vs. dynamic - call stack
What exceptions or cheats are used with the kind of scoping rule(s) JavaScript has?
eval
and wait
What are the different ways you can create a new scope?
Functions, catch
block, (block scope) {} with let
What’s the difference between undeclared and undefined?
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.
What determines which object a function’s this
points to? What’s the default?
In order of precedence
-
new
keyword - Explicit Binding:
.call()
or.apply()
/ hard binding - Implicit Binding: Owning or containing object (obj.function call)
- Default: global unless strict mode is on and then it is undefined
How do you “borrow” a function by implicit assignment of this
?
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 do you explicitly bind this
?
.call()
or .apply()
How can you seal a specific object to a function? Why do that? Why not?
.bind()
- to be predictable for the this
binding, but you loose flexibility.
How do you create a new this
object?
By using the new
keyword
What is a closure and how is it created?
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 long does its scope stay around?
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.
Why doesn’t a function callback inside a loop behave as expected? How do we fix it?
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 do you use a closure to create an encapsulated module? What are the benefits of that approach?
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
What is a constructor?
A function called with the new keyword in front of it.
What is [[Prototype]] and where does it come from?
Linkage from one object to another object - can get from Object.create or from the new keyword.
How does a [[Prototype]] affect an object?
Similar to lexical scope it provides the prototype chain and allows traversal (delegates) for function calls and properties
How do we find out where an object’s [[Prototype]] points to (3 ways)?
.\_\_proto\_\_
, Object.getPrototypeOf()
, .constructor.prototype
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);
this
will point to the button instead of the a1
object because of the call.
How is Javascript’s [[Prototype]]
chain not like traditional/classical inheritance?
Javascript doesn’t copy down for class heirarchy instead it links or delegates up. The arrow is going the opposite direction.
What does “behavior delegation” mean and how does it describe object linking in JS?
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.
Why is “behavior delegation” as a design pattern a helpful thing? What are the tradeoffs?
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.
What is “callback hell”? Why do callbacks suffer from “inversion of control”?
Callback hell is giving over control of your program to some other utility and all the trust that invovles.
How do you pause a generator?
yield
How do you resume a generator?
next()
What is a promise? How does it solve inversion of control issues?
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.
How do we combine generators and promises for flow control?
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.