/    Sign up×
Community /Pin to ProfileBookmark

class name in variable (prototype): how to create instance

Hi All

Suppose I have the following class definition

[code]
MyClass = new Class() ;
MyClass.prototype = {
initialize: function(){},
// and other functions
}
[/code]

Now I would like to create an instance, which you normally would do like

[code]
var obj = new MyClass() ;
[/code]

but I have the class name in a variable:

[code]
var class_name = ‘MyClass’ ;
[/code]

The question is now: using this variable, how do I create an instance. I’ve tried things like

[code]
var class_name = ‘MyClass’ ;
var obj = window[class_name]() ;
[/code]

which, unfortunately, didn’t work ?

Any suggestions ?

thnx
LuCa

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscFeb 27.2009 — EDIT:

My bad, i understand the question now. You're not calling it as a constructor function - did you try that?

<i>
</i>var class_name = 'MyClass';
var obj = [COLOR="Red"]new[/COLOR] window[class_name]();


Or, actually, referring to this thread: http://bytes.com/groups/javascript/530871-how-do-i-call-function-variable-refers, you can just use the variable name:

<i>
</i>var class_name = 'MyClass';
var obj = new class_name();


I haven't tested either of these....
Copy linkTweet thisAlerts:
@jeanlucaauthorFeb 27.2009 — nope, the classname is in a variable!
Copy linkTweet thisAlerts:
@slaughtersFeb 27.2009 — Just use eval

var class_name = 'MyClass' ;

var cmd = 'var obj = new ' + class_name + '() ;'

eval(cmd);
Copy linkTweet thisAlerts:
@jeanlucaauthorFeb 27.2009 — <i>
</i>var obj = new window[class_name]();

doesn't work (I've posted the same, but forgot the 'new')

I guess 'eval' is a good solution!
Copy linkTweet thisAlerts:
@jeanlucaauthorFeb 28.2009 — EDIT:

My bad, i understand the question now. You're not calling it as a constructor function - did you try that?

<i>
</i>var class_name = 'MyClass';
var obj = [COLOR="Red"]new[/COLOR] window[class_name]();


Or, actually, referring to this thread: http://bytes.com/groups/javascript/530871-how-do-i-call-function-variable-refers, you can just use the variable name:

<i>
</i>var class_name = 'MyClass';
var obj = new class_name();


I haven't tested either of these....[/QUOTE]


The first does work (it didn't before because I made an error somewhere else. The second doesn't work!! thnx
×

Success!

Help @jeanluca 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...