/    Sign up×
Community /Pin to ProfileBookmark

How to use variable in namespace

Hi,

Does anybody know how I can use a variable in a namespace?

[CODE]
var variable = ‘bar’;

new GLOBAL.hi.there.variable();
[/CODE]

(above code returns an undefined)

Thanks! ?

Christophe

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@svidgenMar 16.2012 — Each "namespace" needs to be an object -- could be a generic object, function, DOM node, anything that'll hold properties.

[code=php]var com = {};
com.google = {};
com.google.www = {};
com.google.www.search = function(s) {

// 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]
Copy linkTweet thisAlerts:
@Christophe27authorMar 19.2012 — I'm not sure if you understand me (or if I understand your reply)

I try to achieve the following:

[CODE]
// I try to write some code like ...
GLOBAL.foo.bar.[I]variable[/I]

// 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]


Hope this clears things out ?

Christophe
Copy linkTweet thisAlerts:
@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?
Copy linkTweet thisAlerts:
@Christophe27authorMar 19.2012 — Using the array notation works fine for me! thanks!
×

Success!

Help @Christophe27 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...