Sunday, April 22, 2012

JavaScript:Tips and Tricks -2

Global variables-
Java script provides 3 different ways by which you can define a global variable-
1) Var statement - var a = 1;
2) window statement - window.b = 1; -> window is global object which is nothing but a container of all global variables.
3) using nothing - c = 1;
All above will work and will define a,b and c global variables in your javascript.

Semicolon insertion -
JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons.It sometimes inserts semicolons in places where they are not welcome. For example -
Following code will alert a - undefined
function testFunction() {
    var a = getValue();
    alert("a - " + a);
}              
function getValue() {
    return
    {
    status: 1
    };
}
Whereas changing the position of "{" in return statement in the following way gives the correct output-
function testFunction() {
    var a = getValue();
    alert("a - " + a.status);
}              
function getValue() {
    return{
    status: 1
    };
}
a.status - 1

In the first function as Javascript inserts ";" just after return statement, which gives faulty result.


Scope -
JavaScript uses the block syntax, but does not provide block scope, which means a variable declared in a block is visible everywhere in the function containing the block. For example the following function will alert b -2
function testFunction() {
    var a = 1;
    { var b = 2; }
    alert("b - " + b);
}
in c# this b wont be visible out of the block. To avoid confusion it is better to declare all variables at the top of each function.

typeof -
typeof is used to identify the Type of a variable. In Javascript it is little in consistent for different kind o variable. For example -
alert("typeOf(90.8) - " + typeof(90.8)); // - number
alert("typeOf(null) - " + typeof (null)); // - object
alert("typeOf(NaN) - " + typeof (NaN)); // - number
var myArray = new Array();          
alert("typeOf(myArray) - " + typeof (myArray)); // - object
Type of 90.8 is a number and type of NaN is also a number. Moreover the Javascript gives type of NULL as object, which is of course misleading.

parseInt -
parseInt is a function that converts a string into an integer.In javascript it stops when it sees a nondigit, so parseInt("16") and parseInt("16 tons") produce the same result. It would be nice if the function somehow informed us about the extra text, but it doesn't. If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and 9 are not digits, so parseInt("08") and parseInt("09") produce 0 as their result. This error causes problems in programs that parse dates and times. Fortunately, parseInt can take a radix parameter, so that parseInt("08", 10) produces 8.

"+" operator -
The + operator can add numbers and concatenate strings.  If either operand is an empty string(or a string), it produces the other operand converted to a string. If both operands are numbers, it produces the sum.
Example -
alert( 1 + 2); // alert 3
alert(parseInt(1 + "2"));// alert 12

Saturday, April 21, 2012

Closure

Closure is a first class function that is bound to the environment where it is declared.It means we can assign this function to a variable and pass it around and invoke it.Here is an example -
Func myTestFunc = delegate(string variable1)
                                {
                                    return "some value";
                                };

Or using lambda -
Func myTestFunc = variable1 => "some value";
The above function can be assigned to any variable like this -

string myVar = myTestFunc("something");

Lets take some more example and try to analyze the output to understand how closures work.
var myVar = "external Variable";
Func myFunc = delegate(string var1)
 {
               return var1 + myVar;
 };

In the above example the myVar is called as "FreeVariable". A free variable is a variable which is referenced in a function which is not a parameter of the function or a local variable of the function.

What will be the output of this function -

Lets try to calculate it. in "incrementsFunc" i get the function pointer(i used the pointer word here because it helps to visualize it), now i execute this function with input as 5. so my first calculation says
myVar = 1 + 1 -> 2
var1 + myVar -> 5 + 2 ->7
Now i execute same function with input param as 6. Again the calculation will be -
myVar = 1+1 ->2
and var1+myVar -> 6 + 2 -> 8
So i think the answer should be 7 and 8.
What your calculation is? lets see that is there at output-
Its 7 and 9 ------------------------??? How??
Although  myVar is a local variable and is suppose to get flushed out after first call, but still its value was there and caused the result to be 9(6 + 3). Well this is called as free variable in lexical environment.
I think we still didnt get the answer of how?. So actually here is what has happened -

The C# compiler detects when a delegate forms a closure which is passed out of the current scope and it promotes the delegate, and the associated local variables into a compiler generated class. It means the local variables are promoted to heap from stack(without boxing), which explains why the value intact. Making this delegate a class, makes it very easy to pass the function around and make an assignment,invoke it. Each time we invoke the delegate we are actually calling the method on this class and as the variables are on heap the values is actually availble. Once we are no longer holding a reference to this delegate,  garbage collector will play its role.
So that is all about the concept of closure. lets try some exercise to see if we understand it correctly.

1) what will be the output of following -
delegate void Action();
static void Main(string[] args)
{
  int x = 0;
  Action a = delegate { Console.WriteLine(x); };
  a();
}

Ans - 0

2) 
delegate void Action();
static void Main(string[] args)
{
  int x = 0;
  Action a = delegate { Console.WriteLine(x); };
   x =1;
  a();
}
Ans - 1. closure bound to its parenting method body and the local variables in it, thus making any change in x will reflect in this function.


3) What will be the output?
public delegate T Iterator() where T : class;
public static Iterator CreateIterator(IList x) where T : class
{
    var i = 0;
    return delegate { return (i < x.Count) ? x[i++] : null; };
}
static void Main()
{
    var iterator = CreateIterator(new string[3] { "string1", "string2", "string3" });
    Console.WriteLine(iterator());
    Console.WriteLine(iterator());
    Console.WriteLine(iterator());
    Console.WriteLine(iterator());
}

Ans-
string1
string2
string3
null