Convert JavaScript local scope variable to global or pass?
Posted: Sat Apr 19, 2008 12:15 am
Can a local scope JavaScript variable be converted to a global scope? If not how can we pass it's value to a variable not defined in a function?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
(function() {
var q = "sdf";
window.q = q;
})();
(function() {
console.log(q);
})();
console.log is a function that comes with firebug.JAB Creations wrote:I keep getting console not defined errors. I haven't had any success in logging anything to the console...other then errors.Suggestions please?
Attaching a variable to a DOM node (like window) makes it available globally. I often use that method for setting timers and such (so they can be overridden / overwritten by other function calls) - a handy trick.http://javascript.crockford.com/survey.html wrote:Named variables are defined with the var statement. When used inside of a function, var defines variables with function-scope. The vars are not accessible from outside of the function. There is no other granularity of scope in JavaScript. In particular, there is no block-scope.
Any variables used in a function which are not explicitly defined as var are assumed to belong to an outer scope, possibly to the Global Object.
Vars which are not explicitly initialized are given the value undefined.
Vars are not typed. A var can contain a reference to an object, or a string or a number or a boolean or null or undefined.
A new set of vars is made every time the function is called. This allows functions to be recursive.