// since we're actually working in the context of an object, // we can refer to "this" to set static-ish variables, like a search history: if (!this.search_history) { this.search_history = []; } this.search_history.push(s);
// perform the actual "search" ... alert("No results for " + s + "! Try searching with svidgen.com instead!"); } // com.google.www.search()
// you can then use "with" // even though you shouldn't generally be using "with" ... with (com.google.www) { search("for something"); search("for something else"); }
// we should be able to see the search history in the // namespace now alert(com.google.www.search_history.length);[/code]
@svidgenMar 19.2012 — #Yup. I'm pretty sure I didn't understand what you're trying to accomplish. But, given your example:
[CODE]// I try to write some code like ... GLOBAL.foo.bar.variable
// Instead of ...
switch ( variable ) { case 'var1': GLOBAL.foo.bar.var1; break; case 'var2': GLOBAL.foo.bar.var2; break; case 'var3': GLOBAL.foo.bar.var3; break; }[/CODE]
It looks like you're working with an existing GLOBAL.foo.bar object, and you want to refer to some semi-unknown property of that object. You have a variable (which you've called variable) that contains a string referring to the property. So, what I think you're looking for is this:
[CODE]GLOBAL.foo.bar[variable];[/CODE]
That'll get you the property of [I]GLOBAL.foo.bar[/I] indicated by the string or number value stored in [I]variable[/I]. I'd be interested to see more of your code and what problem this is solving. Could you post some of your code?