js-lib

Friday, February 11, 2011

JavaScript 101 - Week 2 (Track 1)



 

Exercise 3.1

function absolute(num){
if (num == parseFloat(num))
  return num < 0 ? -num : num; }
 

Exercise 3.2

function greaterThan(testValue){
return function(number){
return number > testValue;
};
}
var greaterThanTen = greaterThan(10);


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.


The switch statement is more convenient and succinct than multiple elseif statements (notwithstanding those pesky break statements!)
Example:
var state;
var abbr = 'NC';
switch(abbr)
{
   case 'NC':
    state = 'North Carolina';
     break;
   case 'VA':
     state = 'Virginia';
     break;
    case 'FL':
     state = 'Florida';
     break;
 }
show(state);

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


A pure function is one which always returns the same value when given the same arguments, and never has side effects. The show() function is not pure because it produces a side effect.

What do we mean when we say a variable in a function is shadowing a top level variable?


A function may access variables which are defined outside of the function's scope. A top-level variable would be one such variable.

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?


Without an end condition, a recursive function will run in an infinite loop. The repeated function calls would certainly overflow the call stack.

Reflect about the difference between object inheritance and class inheritance


JavaScript is a loosely typed language with no classes, so objects do not inherit from predefined classes.  Instead, new objects can be made that are similar to existing objects, and then customized.  Object customization requires less work than defining classes, and less overhead, too.

What is object augmentation, and how do we do it?


Object augmentation is the addition of new members to existing object instances. It is accomplished through simple assignment.

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?


By using “linkage” new String objects can be created which will have all methods included in the original String. Linkage is achieved through the object() function, as in:
var newString = object(String);

What is garbage collection?


Garbage collection is a means of reclaiming unused computer memory once object references are no longer needed.

What is the difference between an array and an object?


Array inherits from Object, but it also has some unique native members of its own, e.g. concat(), sort() and splice(). A new empty array can be declared using the literal []. The Array links to Array.prototype

 

No comments: