JavaScript Flashcards

Learning core concepts

1
Q

What ultimately dictates the binding for ‘this’?

A

Call time of the function containing this always dictates the binding for this.

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

What are some dangers to consider when working with parameter bindings?

A

Losing parameter bindings is a risk since any function that takes a callback function may call your function differently than you expected, and bind values you weren’t intending, or no values at all.

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

How to determine what the ‘this’ keyword refers to?

A

1.) Scan outward from the keyword this looking for the closest enclosing braces that represent a function body. Note: Ignore braces that don’t represent function bodies like braces for object literals and if blocks.
2.) Having found the function definition, look for where the function is called. In the debugger, you can find that place by looking one step down in the call stack.
3.) Inspect syntax of how the function is being called.
There are only five patterns. Each pattern is associated with a specific rule, which the interpreter uses to determine what this gets bound to inside that function for that one invocation. Once you know which invocation pattern is used, you can predict what the interpreter will bind to this.

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

Where in the debugger can you look to see where a function is called?

A

Look in the call stack section

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

What are the binding patterns for ‘this’?

A

Global reference, free function invocation, .call/.apply invocation, construction mode, method invocation. 90 % of the time, you’ll only use the method invocation rule.

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

What is the method invocation rule you will use ~90% of the time in predicting what ‘this’ gets bound to?

A

“In a function, this is bound to the object to the left of the dot where that function was called.”

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

When would you want to use a decorator function?

A

You’d use a decorator to add some functionality to an object that had some other functionality in it already.

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

What is pseudoclassical code?

A

Like its sister, prototypical code, pseudoclassical strives to resemble the class systems of other languages by adding a thin layer of syntactic conveniences.

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

Do object relationships change when refactoring prototypical code in the pseudo-classical style?

A

No because the psuedoclassical pattern is just a thin layer of syntactic convenience on top of the prototypal pattern.

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

How are properties stores in a pseudoclassical pattern?

A

Similarities in instances of a class are stored as properties ona prototype allowing for class like patterns found in other languages.

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

Where would you code how an instance should be different from all others?

A

Within the functional scope of the constructor function we define how instances will differ from one another.

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

What type of class patterns does JS support

A

Prototypical, psuedoclassical, and functional classes are all supported

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

What is a drawback of the functional style of programming?

A
A big drawback in a functional class below is the creation of a new method for every instance of the function. In exchange for this drawback, however, we get the advantage of having a class that is extremely easy to read and explain and since code clarity is extremely valuable, I recommend using the functional pattern for most classes, provided you won't be creating thousands of instances of that construction which becomes a performance issue. => 
var Car = function(loc) { 
  var obj = {loc: loc};
  obj.move = function() { this.loc++ };
  return obj;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a drawback of the pseudoclassical pattern?

A

There is quite a bit of complexity under the hood. Upside is that JS interperters out there have written massive performance enhancements that target this pattern making it worthwhile to consider. In most cases though, the classes you write won’t be instantiated enough times to take advantage of the speed improvements, so it’s often wise to write in the pattern that willl be simplest to read and understand.

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

What is a good way to think of JavaScript classes?

A

Any construct that is capable of producing a fleet of similar instances conforming to some interface.

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

Describe a functional class pattern with shared methods

A
var Car = function(location)  {
  var obj = {loc: loc};
  extend(obj, Car.methods); 
  return obj; 
};
Car.methods = {
    move: function() {  function() { this.loc++; }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are prototype chains for?

A

They are a mechanism for making objects that resemble other objects. You might decide to copy all the properties over from one object to another. As an alternative, JS provides the option of prototype chains. This makes one object behave as if it has all the same properties of another object, by delegating it’s failed property lookups to that other object at lookup time.

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

What happens when you ask for a property an object doesn’t have?

A

The lookup “falls through” up the chain up to the porotype object. Note tahat similarity is achieved at the very moment of lookup.

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

What is a drawback of one-time copying of objects using methods like extend?

A

Since the copy operation was one-time, the relationship between the two objects was over immediately.

20
Q

What is the object prototype?

A

The top-level object that EVERY object eventually delegates to, where all the most basic methods are provided. We call it “the object prototype”, since it provides the share properties of all objects.

21
Q

Does the Array Prototype delegate to anything?

A

Yes, to the object prototype so that the non-unique aspects of arrays can be inherited from the object constructor, and not every method will need to be re-implemented for arrays.

22
Q

What are the steps for making instances of an object?

A

A ine in the function where you create a new instance object i.e. Object.create(); A delegation from the new object to some prototype, and…some logic for augmenting the new object with the properties that make it unique from other objects of the same class.

23
Q

When are delegation relationships created?

A

ONLY created via use of the Object.create function.

24
Q

How is it best to view the .prototype property?

A

As a freely-provided object for storing things, with no additional special characteristics. Mentally substitute the word methods if it helps you to reason.

25
Q

What is a prototype property on an object?

A

The prototype is a proprety on a constructor function that sets what will become the __proto__ property on the constructed object.

26
Q

What is an object?

A

A collection of properties that has a single prototype object. There also primitive values but when needed, they are also converted to objects. Hence almost everything is or can be expressed as an object.

27
Q

What is a prototype chain?

A

A protoype chain is a finite chain of objects which is used to implement inheritance and share proprties.

28
Q

What is delegation based inheritence?

A

ECMAscript has no ceoncept of classes. However, protypal chains can achieve much the same thing. Delagation based inhereitence is another phrase for prototype based inheritence.

29
Q

Does Object.prototype have its own __proto__?

A

Yes, it is the final link in of a prototypal chain and is set to null.

30
Q

When would we want to use a constructor function?

A

When we need to have objects with the same or similar state structure (same properties) and with idifferent state valus. Constructor functions allow us to produce objects based on specific patterns.

31
Q

What is the difference between prototype and __proto__?

A

prototype is the object that is used to build __proto__ when you create an object with new. prototype is not available on the instances themselves (or other objects), but only on the constructor functions.

32
Q

How many contexts does JS have?

A

There are three types of ECMAScript code: global code, function code and eval code. one function may generate infinite set of contexts, because every call to a function (even if the function calls itself recursively) produces a new context with a new context state

33
Q

What is a context which activates another context called?

A

A caller.

34
Q

Can one execution context activate another?

A

Yes, an execution context may activate another context, e.g. a function calls another function (or the global context calls a global function), and so on. Logically, this is implemented as a stack, which is called the execution context stack.

35
Q

What is a callee?

A

A context is being activated is called a callee. A callee at the same time may be a caller of some other callee (e.g. a function called from the global context, calls then some inner function).

36
Q

How is ECMAScript program runtime presented?

A

presented as the execution context (EC) stack, where top of this stack is the active context:\

37
Q

What is the object-like representation of a execution context look like?

A

Execution contexts can abstractly be represented as objects containing properties for variable object (vars, args, function declarations), for the scope chain (variable objects + all parent scopes) and the thisValue (context object).

38
Q

What is a variable object?

A

A variable object is a container of data associated with the execution context. It’s a special object that stores variables and function declarations defined in the context.

39
Q

Why is ‘this’ useful?

A

it will always ensure that the correct values are used when a member’s context changes.

40
Q

What is polymorphism?

A

The ability of multiple object types to implement the same functionality. Like when a Person object property gets passed down or inherited by instances of it like a Student Object for example which has some of the person properties and then some ones of its own.

41
Q

How is the JS version of a classes implemented?

A

The constructor function is JavaScript’s version of a class. You’ll notice that it has all the features you’d expect in a function, although it doesn’t return anything or explicitly create an object — it basically just defines properties and methods. whenever one of these object instances is created, the object’s name property will be equal to the name value passed to the constructor call,

42
Q

What does create() actually do?

A

you can create a new object based on any existing object => var person2 = Object.create(person1);

43
Q

What is the distinction between object’s prototype and the prototpe property on constructor functions?

A

The former is the property on each instance, and the latter is the property on the constructor. That is, Object.getPrototypeOf(new Foobar()) refers to the same object as Foobar.prototype.

44
Q

What is a base case in the context of recursive functions?

A

The answer to a problem at the lowest possible level

45
Q

What are 2 key aspects of call stacks?

A
  1. It is single-threaded. Meaning it can only do one thing at a time.
  2. Code execution is synchronous.
  3. A function invocation creates a stack frame that occupies a temporary memory.
  4. It works as a LIFO — Last In, First Out data structure.