Saturday, February 5, 2011

Assignment - Week2

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.
  Switch Statements:
Switch statements are easier to debug, easier to read, easier to understand, and easier to maintain. Also, switch statements are faster to execute. In an if-else statement the compiler has to evaluate each if-else statement beginning from top until it reaches the correct statement this does not occur for a switch statement where the compiler is able to optimize the order of processing each individual case statement.

If-elseif-else statement                                              Compiler Statement

if(value==1)                                                                  compare value, 1
            statement1;                                                                  jump if zero label 1
elseif(value==2)                                                           compare value, 2
            statement2;                                                                  jump if zero label2
elseif(value==3)                                                           compare value, 3
            statement3;                                                                  jump if zero label3
                                                                                    label1:
                                                                                                call statement1
                                                                                    label2:
                                                                                                call statement2
                                                                                    label3:


switch(value) {                                                  Assembler Statements
            case 1:                                                             add program_counter, value
                        statement1;                                          call statement1
                        break;                                                  call statement2
            case 2:                                                             call statement3
                        statement2;
                        break;
            case 3:
                        statement3;
                        break;
}

2. What is encapsulation, and what do functions encapsulate?
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields.
So, functions encapsulate not only the data but also the members of the object where object members are used to manipulate an object’s data.

3. What is a pure function? Is the function show() provided in Eloquent Javascript a pure function?
A pure function has the property that it always returns the same value for the same input, and they have no side effects
Yes, the function show() it is a pure function, it should return the same values every time for a given input.

4. What do we mean when we say a variable in a function is shadowing a top level variable?
Java Script does not provide the same variable scooping rules as other OOP languages do. However, the language provides top and local level variable scooping where the same variable can be declared as a global variable and also as a function variable. Then, as the program executes and a function call is made, where a variable is defined in the function but also in the global space, the function level variable is used for the function execution.
 
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?
A recursive function is a method where the solution to a problem depends on solutions to smaller instances of the same problem. This means a function calls itself over and over again until the smallest set, a stopping condition, has been reached.
As each function call is being made the function is not evaluated, no result is calculated, but put onto the system stack. Once the smallest problem to the question, the stopping condition, has been reached, each function call calls itself over and over again where each call is put yet no actual value is return by each call this only occurs once the last ca yet no function call returns the actual value. So, each function call is stored on the system stack s pulled from the system stack, evaluated, the result handed to the next call, and so fort until all calls have been pulled of the stack and the final result has been calculated.
Now assume the smallest problem, or stopping condition, does not exist an infinite number of function calls would be put onto the system stack, so many, until the stack runs out of memory.

6. Reflect about the difference between object inheritance and class inheritance
The classical approach to creating an object is to define the structure of an object, using a class declaration, and instantiate that class to create a new object. Objects created in this manner have their own copies of all instance attributes, plus a link to the single copy of each of the instance methods.
In prototypal inheritance, instead of defining the structure through a class, you simply create an object. This object then gets reused by new objects thanks to the way that prototype chain lookups work. It is called the prototype object because it provides a prototype for what the other objects should look like.

7. What is object augmentation, and how do we do it?
            New members can be added at any time to an object by simple assignment
                        object.attribute=<value>

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?
According to some online search we will need to use JavaScript’s “Prototype” property. Using this property allows us to add custom made methods/functions to any type of Object.
1)      Define a method/function that you would like your object to have. For it to work properly you will need to refer to the object method as “this” within the function.
function <name> () {
Expressions
}
2)      Then, use the String object prototype to define your method/function and assign the method/function name to the defined method/function
             String.prototype.<method-name>=<method-name>
3)      Now, create a string object
var msg1 = new String(“Test String1”);
msg1.<method-name>();  

      or
var msg2 = “Test String2”;
msg2.<method-name>();
4)      You can either use a return statement to return a value in the method/function or not and do all work within the method/function

9. What is garbage collection?
In any OOP language whenever a variable, array, or object is being created. Once that array, object, is not used anymore than that memory needs to be reclaimed or after a while, similarly as in recursion, after too many unused objects are taking up space we will run out of memory. For this reason JavaScript provides garbage collection where arrays, objects, that will not be used anymore are removed from memory.

10. What is the difference between an array and an object?
In JavaScript Objects and Arrays are very similar, they can be defined and created the same way:
    var arr = new Array();
    var obj = new Object();
However, an array is used to represent an ordered collection of numbered values, whereas an  object is used to represent an unordered collection of named values, or what is known as an associative array
Also, an array has a length method, and an array can inherit attributes from an object but not vice versa.


Hi All,
I added the link to the programming assignments
http://jsfiddle.net/swissrunner66/2YjFm/
To execute each program you need to uncomment the: <!-- -->


No comments:

Post a Comment