Monday, February 07, 2011

JavaScript:101, Week 2, Reflections

Questions for reflection:
  1. 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.

  2. 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.
    It should be noted though that is JavaScript you can access variables that are outside the function (1 step above). So for example I can have a string that says "foo" in the main JavaScript area and then inside of a function say that this variable is now "bar". The version outside of the function is not changed.

  3. What is a pure function? Is the function show() provided in Eloquent Javascript a pure function?

    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.

  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.

1 comment:

  1. Good, understandable answers. Thanks