1. In Javascript, functions are first class objects. What does it mean to be a first class object?
Answer: Functions are first class objects. This means they have all the properties of a normal JS object: they can contain name/value pairs, can be passed, returned or stored just like objects.
2. Functions and variables share the same namespace. What does this mean and what important implication does this have?
Answer: According to Wikipedia, "a namespace is a container that provides context for the identifiers it holds". Hence, a namespace basically stores the names of the names of variables, objects, etc. To share a namespace means that the names of functions and variables are stored in the same space in Javascript. This means JS cannot differentiate between a variable and a function with the same name. Thus, they can overwrite each other and cause serious errors in the code. Hence, we should never have the same name for a variable and a function.
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'?
Answer: Constructs, in general, refer to building blocks. In JS, as functions behave as first class objects, we can pass a function to another function or return functions from other functions. Thus, similar to objects, a function inside a function is only accesible to that function. This makes our code more secure and prevents other functions or variables from interfering with that block of code. Thus, the program can run smoothly as small independent entities.
4. Can you explain the concept of a closure.
Answer: Closure means a function, even after being by another function, can still be accessed from the parent function and vice-versa. This means one function can access all the variables inside the other function even after one has been called by the other. We can call the inner function with different values of arguments as many times we want but there will be no problem. (In the video, it is explained very well with the help of an example so not elaborating any more.)
5. What is the difference between a function and a method?
Answer: Functions are methods are basically the same thing with the difference that methods are functions that is associated with a object i.e., they are function sinside an object. In Javascript, it is difficult to differentiate between the two as there are no classes and most functions are in turn associted 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?
Answer: In JS, functions have a special parameter called "arguments" which holds all the arguments from invocation and can be traversed with the help of a loop as it contains a special length property (argument.length). Thus, each and every argument can be checked individually with the typeof operator to check if it is of the desired type and also, the number of arguments can be checked with the length property.
example:
function num(n){
if((typeof n)!="number")
{alert("Error: n is not a number!!!");
break;
}
...........
}
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.
Answer: The operator 'this' allows us to access variables inside a function and gives methos access to their objects. It is decided during function invocation what will this point to. Depending on the type of function invocation, this points to:
Well, finally catching up after two hectic weeks. I will be putting up the assignments soon; got a lot of catching up to do.
Answer: Functions are first class objects. This means they have all the properties of a normal JS object: they can contain name/value pairs, can be passed, returned or stored just like objects.
2. Functions and variables share the same namespace. What does this mean and what important implication does this have?
Answer: According to Wikipedia, "a namespace is a container that provides context for the identifiers it holds". Hence, a namespace basically stores the names of the names of variables, objects, etc. To share a namespace means that the names of functions and variables are stored in the same space in Javascript. This means JS cannot differentiate between a variable and a function with the same name. Thus, they can overwrite each other and cause serious errors in the code. Hence, we should never have the same name for a variable and a function.
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'?
Answer: Constructs, in general, refer to building blocks. In JS, as functions behave as first class objects, we can pass a function to another function or return functions from other functions. Thus, similar to objects, a function inside a function is only accesible to that function. This makes our code more secure and prevents other functions or variables from interfering with that block of code. Thus, the program can run smoothly as small independent entities.
4. Can you explain the concept of a closure.
Answer: Closure means a function, even after being by another function, can still be accessed from the parent function and vice-versa. This means one function can access all the variables inside the other function even after one has been called by the other. We can call the inner function with different values of arguments as many times we want but there will be no problem. (In the video, it is explained very well with the help of an example so not elaborating any more.)
5. What is the difference between a function and a method?
Answer: Functions are methods are basically the same thing with the difference that methods are functions that is associated with a object i.e., they are function sinside an object. In Javascript, it is difficult to differentiate between the two as there are no classes and most functions are in turn associted 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?
Answer: In JS, functions have a special parameter called "arguments" which holds all the arguments from invocation and can be traversed with the help of a loop as it contains a special length property (argument.length). Thus, each and every argument can be checked individually with the typeof operator to check if it is of the desired type and also, the number of arguments can be checked with the length property.
example:
function num(n){
if((typeof n)!="number")
{alert("Error: n is not a number!!!");
break;
}
...........
}
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.
Answer: The operator 'this' allows us to access variables inside a function and gives methos access to their objects. It is decided during function invocation what will this point to. Depending on the type of function invocation, this points to:
- Method form(thisObject.methodName(arguments))- this is set to thisObject i.e., the object containing the function.
- Function form(functionObject(arguments))-this is set to the global object..
- Constructor form(new functionObject(arguments))- a new object is created whenever new operator is used and this points to it.
Answer: Typecasting refers to the conversion of one data type to another. It can be both implicit or explicit. In certain languages like C++, explicit type casting can be used to convert one data type into another explicitly i.e., by using the cast operator. However, in JS there is no explicit type casting i.e., the user does not need to specify when dealing with different data types. Hence, cast operator is not required.
9. What is reflection and why is it easy in Javascript (you may need to do research to answer this question)?
Answer: Reflection is when the code looks onto itself to discover its variables and functions. In Javascript, since basically everything is an object, reflection is easier as we can use a simple loop to check all variables and functions in an object without knowing their properties. Reflection is very well explained in this article.
Well, finally catching up after two hectic weeks. I will be putting up the assignments soon; got a lot of catching up to do.