
Originally Posted by
???
I was just wondering if it was possible to make a global variable from inside a function.
A variable, in a literal sense (see your deletion thread), can only be defined using a var declaration, therefore the statement must occur outside any function. However, a property can be created at any time, provided you have a reference to the object.
Code:
var global = this; // You can use window, but I prefer not to.
function foo() {
global.property = 'value';
}
If the variable is predictable, I would suggest that you just declare it and reserve the above for cases where run-time creation really are necessary - for example, when the property name is chosen dynamically. Using a declaration makes it clear what variables exist, rather than forcing one to look through every line of code for an assignment.
Bookmarks