Questions for reflection:
- Why do languages provide the switch statement, when we can achieve the same thing with multiple if... elseif statements? Show one example of how you might use the switch statement.
Reasons to use switch statements include: better readability, easier to debug/maintain, faster execution is the compiler has that capability.
- What is encapsulation, and what do functions encapsulate?
Encapsulation basically means that things inside of a function are separate from those things outside of it.
A pure function doesn't affect anything outside of its self. Meaning a pure function doesn't change the value of any global variable or anything like that.
- What do we mean when we say a variable in a function is shadowing a top level variable?
When a variable is referenced in a function it looks for it inside the function first, then outside the function. This means you can have a value inside the function with the same name as one outside the function and know that the one inside will be the one used.
- A recursive function, must have some sort of an end condition. Why would we get a "out of stack space" error message if a recursive function does not have an end condition?
If there is no end condition the function will just keep getting called 'infinity' until there isn't enough memory to have more instances of it on the stack.
- Reflect about the difference between object inheritance and class inheritance.
Object inheritance allows you to basically make a copy of an object that you expand upon. Class inheritance means that you need to define a new class tat extends a previous class and then create objects of that class.
- What is object augmentation, and how do we do it?
In JS you can add members to any object at any time by assignment. This can be very useful if you need to add a new property or function to something.
- There is a way to add a method to String, such as any new String we create will have that augmented method (this is a bit different from object augmentation). How would you do this?
You can add a method to array.prototype and it will be added to all arrays. Strings are an array of letters.
- What is garbage collection?
Garbage collection means that the js environment clears the memory for items that are no longer referenced in the program. Meaning that you aren't wasting system memory on objects that you've already deleted or that were created inside of a function call.
- What is the difference between an array and an object?
An array links to array.prototype and therefore has array functions. It also has a .length that is meaningful. An object links to object.prototype and therefore has object functions. Use arrays when you need to access things according to order, use objects when you want to access things by assigned names.