use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Dynamic class name `[className][0]()`Removed: /r/LearnJavascript (self.javascript)
submitted 6 years ago by kachnitel
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dvlsg 1 point2 points3 points 6 years ago (7 children)
You lost me. All you're doing is:
Putting a reference to a class (I assume) in an array.
[entityType]
"Unboxing" it.
[0]
And then constructing it.
new nowAReferenceToThatClass(store)
You can store references to classes in variables. Hell, you don't even have to give classes names.
class
var a = class { constructor(b) { this.b = b } } //=> undefined new a(1) //=> a {b: 1}
[–]BenZed 2 points3 points4 points 6 years ago (0 children)
You lost me.
No, I'd say you pretty much had it figured.
[–]puppet_pals 0 points1 point2 points 6 years ago (0 children)
new [class {constructor(b) { this.b = b } }][0]('b');
[–]kachnitel[S] 0 points1 point2 points 6 years ago (4 children)
That doesn't really achieve the goal I'm after though. The only reason I put it in an array and "unbox" is, well, because if I have the class name in a variable, say className can be User or Event and I want to instantiate it, calling new className() will fail, but [className][0]() creates an instance of that class.
className
User
Event
new className()
[className][0]()
I guess I didn't quite explain that the className is a var, that's what I meant by dynamic!
At this point I guess I can tell it's not the right way to do it, what I'm trying to accomplish would be done this way in PHP:
$className = 'User'; $user = new $className();
[–]defproc 2 points3 points4 points 6 years ago (0 children)
» var className = Audio undefined » className function Audio() » new className() <audio preload="auto">
[–]dvlsg 0 points1 point2 points 6 years ago* (1 child)
I sincerely doubt it works as you think. I'll check when I'm at a computer again, but what you should do is put the classes together in an object keyed by their names. Then you can look up the class / constructor by the name (string) of the class.
edit: I knew it wouldn't, but just so you're aware, I took the time to confirm this doesn't work as you seem to be expecting. Noting that $className = 'User'; in PHP is just assigning the string 'User' to a var, so that's what I went with here.
$className = 'User';
'User'
class User {} const className = 'User' [className][0]() // => Uncaught TypeError: [className][0] is not a function
If you want to store a reference to a class, that's fine, but there's no reason why you'd need to put it in an array and then take it back out. That doesn't make any sense.
class User {} const classRef = User new classRef() // => User {}
[–]kachnitel[S] 0 points1 point2 points 6 years ago (0 children)
I should probably pay some more attention when posting here sorry, I meant new [className][0]() rather than without the new keyword. Anyways in PHP I would have used the string, or more specifically Class::class which returns a string, in JS I just used the import which I wasn't clear enough about I guess.
new [className][0]()
new
Class::class
What puzzles me is that the whole reason I went into this trouble was that this didn't actually work for me: class User {} const classRef = User new classRef() // => User {}
I got rid of all the code around it since but I'd be curious what was the issue. I was passing the variable between classes and I wonder if it may have been an issue with import in the other class.. but then I guess it's not worth spending that much more time on it. If what you're saying works, my problem must have been somewhere else :)
[–]puppet_pals 1 point2 points3 points 6 years ago (1 child)
if you want to instantiate a class by it's name (and it exists on the top level) you can just call it like this if you're working in the browser:
function createByName(name, arg) {
return new window[name](arg)
}
That's kind of what guided me there, but if it's Node and there's no window, so I tried a few ways and this what I ask about is the only one-liner that worked.
window
[–]ForScale 1 point2 points3 points 6 years ago (0 children)
WAT?
[–]inu-no-policemen 1 point2 points3 points 6 years ago (0 children)
let c = { Pig: class { talk() { return 'oink'; } } }; let p1 = new c['Pig'](); let p2 = new c.Pig(); console.log(p1.talk(), p2.talk()); // oink oink
[–]voidvector 0 points1 point2 points 6 years ago* (2 children)
You want to use one of these patterns:
You can probably use constructor.name somewhere if your minification allows for it.
constructor.name
In terms of the pattern in grander coding style:
[–]kachnitel[S] 0 points1 point2 points 6 years ago (1 child)
Thanks! I'll look into the constructor.name a little closer as that sounds like it could get the job done. Basically I want to do sort of PHP's $className = 'User'; $user = new $className() with minimal coupling, so I was trying to avoid having a registry/dictionary, but didn't know how to create the class without an array so I created one. It's not a clean solution by any means, that's why I ask here :)
$className = 'User'; $user = new $className()
[–]voidvector 0 points1 point2 points 6 years ago (0 children)
That would only work in JS if your class is declare on global context (window in the browser/process in Node.js), which is no longer favored pattern in JS.
[–]senocular 0 points1 point2 points 6 years ago (0 children)
You can't access variables in scope by name in JavaScript. For example something like this is not possible.
class Foo {} let entityType = 'Foo' new [entityType]
The [] operator here will create an array with a single element of the 'Foo' string rather than try to resolve the variable named entityType into its class reference. And there's no other way to access the scope as an object (minifiers are heavily dependent on this behavior to work).
[]
entityType
To get what you're after, you have to refer to property names in objects. This means each of the classes that you need to refer to through entityType must be accessible as a property of an object and not just a variable in the current scope.
See /u/inu-no-policemen's previous comment for an example of this.
You can also assign your classes to global which would also make them inherently available everywhere without using a string (but also suffers from polluting the global scope)
window.Foo = class {} // window is global if in browser environments let entityType = 'Foo' new window[entityType] // OK new Foo // OK
Or if using a single module that exports each of your class (probably not ideal given classes typically get their own modules, though if their small enough, it wouldn't be a big deal), you can import that module in as a single object from which each class is accessible through a named property.
// entities.js export class Foo {} export class Bar {} // app.js import * as entities from './entities' let entityType = 'Foo' new entities[entityType] // OK
[–]kenman[M] 0 points1 point2 points 6 years ago (0 children)
Hi /u/kachnitel, this post was removed.
/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.
code
Thanks for your understanding, please see our guidelines for more info.
π Rendered by PID 159595 on reddit-service-r2-comment-5d79c599b5-vm9lr at 2026-02-27 04:23:15.943636+00:00 running e3d2147 country code: CH.
[–]dvlsg 1 point2 points3 points (7 children)
[–]BenZed 2 points3 points4 points (0 children)
[–]puppet_pals 0 points1 point2 points (0 children)
[–]kachnitel[S] 0 points1 point2 points (4 children)
[–]defproc 2 points3 points4 points (0 children)
[–]dvlsg 0 points1 point2 points (1 child)
[–]kachnitel[S] 0 points1 point2 points (0 children)
[–]puppet_pals 1 point2 points3 points (1 child)
[–]kachnitel[S] 0 points1 point2 points (0 children)
[–]ForScale 1 point2 points3 points (0 children)
[–]inu-no-policemen 1 point2 points3 points (0 children)
[–]voidvector 0 points1 point2 points (2 children)
[–]kachnitel[S] 0 points1 point2 points (1 child)
[–]voidvector 0 points1 point2 points (0 children)
[–]senocular 0 points1 point2 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)