Sunday, February 13, 2011

Assignment - Week3

  1. In Javascript, functions are first class objects. What does it mean to be a first class object?
    This means that javascript functions are special type of object that can do all the things that regular objects can do This ssupports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions. 

  2. Functions and variables share the same namespace. What does this mean and what important implication does this have?
    By definition, a namepace is an abstract container to hold a logical grouping of unique identifiers or symbols which are unique to a specific namespace but have no correlation within another namespace. Moreover, in javascript, functions can be defined just like variables, meaning, a variable can reference a simple numeric value but also reference a complete function. So, if we declare a global variable var x and function local variable var x the, when we execute the program and the function is executed, the function local variable x with shadow the global variable x. This can lead to incorrect evaluations where the variable x is involved. 

  3. Douglas Crockford equates Javascript functions with Lambdas, and also mentions that they are a secure construct. Can you do some research and reflect on what he means by 'secure construct'?
    Haven't found any good explanation to answer this question sufficiently.

  4. Can you explain the concept of a closure.
    In JavaScript a closure is the local variable of a function kept alive after the function has returned. This form of “closure” occurs when creating a function within another function where the local function variables are still accessible after the inner function has returned.

  5. What is the difference between a function and a method?
    In JavaScript a function is a piece of code that is called by a specific name. The function accepts and passes data, parameters, to operate on and uses a return statement to return a value to the caller.
    A method, on the other hand, has also a specific name but its name is associated with an object. 
     
  6. In Javascript there is no implicit type checking of arguments passed to functions. This could lead to bugs if programmers are not careful about what they pass to functions. If you create a function, how would you protect it from well meaning but careless programmers?
    One way is to check the number of arguments passed to the function. The parameter list is stored in an object/array “argument” and so its length can be called.
    Also, the programmer can pass an object, containing the name/value pairs of the function, as a parameter to the function.

  7. Javascript functions have implicit access to something called this. this points to different things depending on how the function was created. Can you explain why we need this, and what it represents in different type of functions.
    The “this” refers to the owner of the function we're re executing, meaning, “this” refers to the object that the function is a method of.

  8. Why doesn't Javascript have a cast operator?
    Unlike in many other programming languagetype to be defined, in javascript no type declaration is required
    var value=10, var hello=”Hello”, var set = function() {statement}
    None of the above declarations require a type. Yet JavaScript knows how to evaluate each declared variable. 

  9. What is reflection and why is it easy in Javascript (you may need to do research to answer this question)?
    Reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime. In non-reflective languages instructions are executed and data is processed. In javascript, however, program instructions are also treated as data, and so allows for reflective program modifications.
    It is easy in JavaScript because the language allows to add new members/values dynamically to an object/array without the specific member to have been defined as the object/array was created.

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: <!-- -->


Sunday, January 30, 2011

Week1

  1. What is a type, and why do you think types are useful in writing programs?
    A type specifies the type of a specifically declared variable/string/array/object
    It refers to a specific value, number/boolean/string/array/object a user/program can define and manipulate in a program.

  2. Why do we lose precision when performing operations with decimal numbers in Javascript? Can you think of a few implications of why this would be a problem?
    In javaScript numbers are stored as 64bits. Some of those bits are reserved based on the numerical value, which means there are only some 50bits available to encode a numerical value.
    Precission is lost for decimal values if the complete decimal value cannot be stored in the available bits.
    For example the number of PI has an infinite number of decimal values and so the complete number cannot be stored in the available bits and so some precision is lost.

  3. Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500
    javaScript, like many other programming languages, support order of precedence, ie. the order of which mathematical operations are performed.
    In javaScript we follow the order:
      "PEMDAS", → Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction
      (((115*4) – 4) + (88/2)) = 500
  1. What is the use of a backslash character in a String?
    A backslash in a string indicates that the character following the backslash has a special meaning. Special meaning of characters can be looked up from the ASCII table.

  2. What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?
    typeof Specifies the type if the operand passed to the operator
    typeof 4.5 Returns the type of the variable → number
    typeof(typeof(4.5) Returns the type of the returned operand type
    typeof(typeof(4.5) → number) → string

  3. The author writes When your browser loads a page, it creates a new environment and attaches these standard values to it. The variables created and modified by programs on that page survive until the browser goes to a new page.. Without searching on the net, can you think of some variables that might be created in the browsers environment when it loads a page with Javascript in it?
          I assume that everything that has been defined between the tags <script> …... </script> →                    javaScript are created when the object window is loaded. So far, in our small programs we have  
              used variables like var, used methods like promp/write/functions/etc.
             All this is loaded when the window loads.

Coding Assignments Week1
http://jsfiddle.net/swissrunner66/Lnc26/9/