Tuesday, 7 June 2011

Javascript week 4 questions for reflection

1.      In JavaScript, functions are first class objects. What does it mean to be a first class object?

In JavaScript, functions can be passed, returned and stored just like any other value.
First class object is an entity which is constructed dynamically, passed as parameter, returned from method and assigned to variable.
E.g. function attachEventToBtn () {
btnOk.click = function () {
                alert (‘Click’);
}
}
                E.g. var vResults = validateFileName ();

2.      Functions and variables share the same namespace. What does this mean and what important implication does this have?

Function and variables share the same namespace means, function and variables can have same name.
E.g. var foo = function foo() {
  alert('Yes');
};
foo();
alert(foo); 

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'?

As Douglas Crockford mention JavaScript functions are source to enormous expressive power and it’s secure. Secure in the sense that functions have well defined scopes, which keeps code enchant. 

4.      Can you explain the concept of a closure?

Closure means even after parent function is returned, inner function defined with the parent function can have access to parent function variables.
E.g. function attachEventToBtn () {
btnOk.style.display = ‘none’;
btnOk.parentNode.innerHTML = ‘<input type="button" id=”btnSave” value="Save"/>’ + btnOk.parentNode.innerHTML;
btnSave.click = function () {
                alert (‘Click’);
btnOk.click();
}
}

5.      What is the difference between a function and a method?

Function can be referred as variable, which can be stored & return value and used as parameter.
As function is used like value, it can be stored in Objects. And a function in objects is called as Method.

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?

As JavaScript is loosely typed language, we cannot declare argument type in function declaration. If we pass wrong type, it will break the code. In order to protect the code we can use the typeof () operator to check the type of argument passed 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?

In most of the languages this refers to the current object initiated by class. In JavaScript this refers to the object owned by the method, but it depends on how it is called.
function setStyle(){
this.style.color='Gray';
}
In above case setStyle will refer to window
element.onclick=setStyle()
In above case it will refer the element

No comments:

Post a Comment