Method 1: Explicitly declare it outside of any function scope:
Method 2: Implicitly use a global variable by not using 'var' inside the function scope:
function x() {
var a = "bye"; // This is only defined in the scope of x
g = "hello"; // g was not var'ed, so it's a global
}
x(); // Run x
alert(g); // alerts "hello" because g is global
alert(a); // a is undefined |
|
Method 3: Define a variable in the window scope:
function x() {
window.g = "hello";
}
x();
alert(g); |
|
No comments:
Post a Comment