Tuesday, 31 May 2011

Javascript week 3 questions for reflection

1.      What is encapsulation, and what do functions encapsulate?

Encapsulation means variables defined in a scope are not visible outside the scope.
Function scopes can create encapsulate, as variables declared in functions are not visible outside of the function. You can use anonymous function to wrap your application.

2.      What is a pure function?

The defining properties of pure functions are that they always return the same value when given the same arguments, and never have side effects. They take some arguments, return a value based on these arguments, and do not monkey around with anything else.

3.      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?

Recursive function calls same function until the end condition is met. When a function is called, memory space is allocated for the local variables within the function and they are pushed in the stack. When function exit, the space on the stack is removed.
If recursive function does not have end condition, stack will go on adding local variable and we will reach out of space on stack.

4.      Reflect about the difference between object inheritance and class inheritance

Class Inheritance:
·         There are classes and instances. Both are two different entities
·         Classes are defined and initiated with constructor method.
·         Construct an object hierarchy by using class definitions to define subclasses of existing classes.
·         Inherit properties by following the class chain.
·         Class definition specifies all properties of all instances of a class. No way to add properties dynamically at runtime.

Object Inheritance:
·         Every object is instance.
·         Set of objects are defined and created with constructor functions.
·         Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
·         Inherit properties by following the prototype chain.
·         Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects. 

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

Object augmentation is a process, in which new members can be added to any object by simple assignment. There is no need to define new class, as there are no classes in JavaScript.
Suppose there is an object  :
 var vObj = {
Id : 1,
 State: “Maharashtra”
} ;
                I can add another object or property to this object at same time or later on:
var vObj = {
Id : 1,
 State: “Maharashtra”,
City : {  CityId : 12,
Title: “Pune”}
} ;


No comments:

Post a Comment