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.

No comments:

Post a Comment