Click here to Skip to main content
15,883,745 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
JavaScript
var ninja = { 
  yell: function(n){ 
    return n > 0 ? ninja.yell(n-1) + "a" : "hiy"; 
  } 
}; 
console.log( ninja.yell(4) == "hiyaaaa", "A single object isn't too bad, either." ); 
 
var samurai = { yell: ninja.yell }; 
var ninja = null; 
 
try { 
  samurai.yell(4); 
} catch(e){ 
  console.log( false, "Uh, this isn't good! Where'd ninja.yell go?" ); 
}

now samurai.yell(4) making exception and catch block is running.
Now look another expample this
JavaScript
var obj={name: "faizan", age:31}
    var obj2 = {name:obj.name};//obj2 pointing to the same memoray location of obj
    console.log("before making null obj::",obj2.name);
    console.log(obj==obj2,"::checking obj==obj2 while var obj2=obj");
    obj=null;  //obj became null
    console.log("after making null obj::",obj2.name);//now this will need to be null but is working why??

obj null now; but obj2.name is showing me faizan . why??
and
the above example working conversily samurai.yell(4); making exception and catch block is running.
I think no differece b/w above examples but the behaviour of both example are not same why??
Posted
Updated 6-Feb-14 19:41pm
v2

In the same scope, var and non-var declared variable won't make any difference. A variable is the "container for an object". As soon as you assign some different object to a variable (it could be the object of different type), you loose the access to the old object, unless it was preserved somewhere else.

The cases where var makes some difference are explained here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var[^].

The difference is made when you use the same variable name in outer scope and, below its declaration, in sub-scope. Then the same name could be interpreted as the same variable as it was in the outer scope, or (with "var") — a reintroduced one, unrelated to the first one.

—SA
 
Share this answer
 
Comments
Abhinav S 3-Feb-14 2:45am    
5!
Sergey Alexandrovich Kryukov 3-Feb-14 2:51am    
Thank you, Abhinav.
—SA
Muhamad Faizan Khan 4-Feb-14 0:17am    
samurai.yell(4); is not working why??
Sergey Alexandrovich Kryukov 4-Feb-14 0:24am    
Perhaps I'll try to answer, but... describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. As simple as that...
—SA
Muhamad Faizan Khan 4-Feb-14 0:50am    
samurai.yell(4) not working while in below example obj2.name is working why? whats the difference
var obj={name: "faizan", age:31}
var obj2 = obj;//obj2 pointing to the same memoray location of obj
console.log("before making null obj::",obj2.name);
console.log(obj==obj2,"::checking obj==obj2 while var obj2=obj");
obj=null; //obj became null
console.log("after making null obj::",obj2.name)
Its the same object. You just set it to null.
Objects work by reference and thus point to the same reference in memory.
 
Share this answer
 
Comments
Muhamad Faizan Khan 4-Feb-14 0:18am    
samurai.yell(4); is not working why??
Sergey Alexandrovich Kryukov 4-Feb-14 1:04am    
Sounds somewhat illogical. After assigning to null something which wasn't null, how can it be the same object? :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900