Functions Scope In Javascript
In Pro JavaScript with Mootools book I have found the below line The scoping rules for function expressions are a bit different from function declarations because they depend on
Solution 1:
As per my understanding in first block of code b and c should be global variables
They are, but they won't have values assigned to them until a
is called (and until b
is called in the case of c
).
Here is the fiddle
That code is different. You have var b
, which makes b
a local variable and not a global.
var a = function(){
b = function(){ c = function(){ alert("b"); }; };
};
a();
b();
alert(typeof a);
alert(typeof b);
alert(typeof c);
Solution 2:
Google the key word below, it is a good place to master scoping and other javascript pattern techniques. It is a series of articles with 12 parts. There are no direct links on the website so you need search for the rest of the articles using part number
search for elegant code basic javascript part 1 elegant code basic javascript part 2 etc...
Post a Comment for "Functions Scope In Javascript"