Friday, May 18, 2012

Hoisting Concept

Well I had the knowledge about variable scope in javascript but did not have the knowledge about hoisting, Two kind of variable scope we can get inside the javascript, first is global and other is local scope. 
The scope of variable is existing inside and outside of the function is called local and global scope respectively. 

In language C and the kind of language, a variable scope is rely under the opening and closing curly braces. In javascript the variable scope is live on under the function. for example 
function example(){
   var i = 100;
   if(i==number){
       if(i == 100){
           var j= 900;
       }
   }else(){
       var k=80;    
   }
} 
Inside the example() function all the variable i, j and k have same variable scope.  Which means a variable we can get inside the function before that variable is declared, the process is known as 'hoisting', which means the variable do behave like they all are declared at very start of function. 

Lets understand it with example. Before that we should know one thing, If we declared the variable without 'var' keyword then its behave like a global variable of particular object under it's chain. 
 Here is the example of hoisting 
var mycar = 'bmw'
function example(){
   alert(mycar);
   var mycar = 'toyata';
   alert(mycar);
} 

It seems that first alert would be the value with 'bmw' but its not like this but it would be alert with value 'undefined' because of hoisting concept(because it's behave like the variable is declared at very start of function). The first 'mycar' would be  hidden because with the same name another variable is declaring inside the function. 

At first alert of 'mycar', the value 'totyata' is not alerting because the line(var mycar = 'toyata';) of function is not executed yet. The 'toyata' would be displayed into second alert. 

It is good practice to declare the variable near where the variable would be used.

Saturday, May 12, 2012

Wrapper Object

In JavaScript everything is about method and property.  There is different type of object. Wrapper object is also one of them. Every object has property and method. So what is the different concept in wrapper object, There we can get the different property and method for string, for example
var myVal = “hello”;
myVal.length;
myVal.indexof(‘h’);
Here myVal is not object however we are using property(length) and method(indexof) to reach that object for particular task. It happens all because of wrapper object concept; 
In simple words when we call the property and method for particular string, at the same time there will be created a temporary object (which is called wrapper object also). When myVal.length is invoked then the temporary object would be made
.
Lets see what happens after created the temporary object with example,
var mystring = “sumanbogati
var newval = mystring.length;
alert(newval)

After executed the line var newval = mystring.length;” the temporary object(wrapper object) would be deleted. One more thing is interesting that we can not assign the value to property of string, for example

var mystring = “sumanbogati”
mystring.length = 5
var newval = mystring.length;

The new value would not be 5 but 10 original one.
The same concept would be apply to number and boolean also.