Convert JavaScript local scope variable to global or pass?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Convert JavaScript local scope variable to global or pass?

Post by JAB Creations »

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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Convert JavaScript local scope variable to global or pass?

Post by Weirdan »

For javascript running in browser "global" scope would be window:

Code: Select all

(function() {
  var q = "sdf";
  window.q = q;
})();
(function() {
   console.log(q);
})();
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Convert JavaScript local scope variable to global or pass?

Post by JAB Creations »

I keep getting console not defined errors. I haven't had any success in logging anything to the console...other then errors. :roll: Suggestions please?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Convert JavaScript local scope variable to global or pass?

Post by Zoxive »

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. :roll: Suggestions please?
console.log is a function that comes with firebug.

Highly recommended when playing around with javascript/css.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Convert JavaScript local scope variable to global or pass?

Post by Kieran Huggins »

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.
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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Convert JavaScript local scope variable to global or pass?

Post by JAB Creations »

I found a way around the issue, thanks for the clarifications.
Post Reply