26

Is it possible to create a new object using a string? For example, how can I convert the string "product" to var p = new Product?

Thanks in advance.

EDIT

What I want to do is to have a menu with <a href="#home"></a><a href="#products">products</a> and create the corresponding object from the href each time.

4
  • what do you hope to do with it? Commented Mar 21, 2012 at 11:44
  • 1
    What's the rule to convert product to var p = new Product? I can understand new Product, but why p? Commented Mar 21, 2012 at 11:44
  • @chchrist: That sounds dangerous, especially if the string comes from the user... why do you think that is this a good idea? Have you considered any alternative approaches? Commented Mar 21, 2012 at 11:44
  • possible duplicate of Instantiate a JavaScript Object Using a String to Define the Class Name Commented Sep 23, 2015 at 19:44

3 Answers 3

76

If you know the context, yes. Let's say you're in a browser environment and Person is a global constructor. Because any global variable is a property of the global object, it means you can access to Person through the global object window:

var p = new Person()

Is equivalent to:

var p = new window.Person()

So you can use the square bracket notation:

var p = new window["Person"]();

Of course this is valid for every kind of object. If you don't want pollute the global scope, you can have:

var mynamespace = {};

mynamespace.Person = function Person() {..}

var p = new mynamespace["Person"]();
Sign up to request clarification or add additional context in comments.

4 Comments

That's a much nicer solution than mine +1
I've tried to create an object using new window["Namespace.MyClass"](), but looks like it's not working. How can we create objects when class contains in modules?
@NarendraV try new window["Namespace"]["MyClass"]() ? (This is just a guess, I don't know that this will work)
@NarendraV, you can use the square bracket notation instead of dot notation, not both: you basically tried to get a property named "Namespace.MyClass" from window where what you were looking was get Namespace from window, and then from Namespace, MyClass. @MattFellows shown the proper way to do so. You can also create a function like: function getReference(string, root) { root = root || this; return string.split(".").reduce(function(ref, item) { return ref[item] }, root) };, and use as var o = new getReference("Namespace.MyClass")(). it's a very simple one, you can make it better.
7

Well you could always use the hideously insecure and illadvised eval(). e.g.

var myStr = "Product"
var p = eval("new " + myStr + "()");

or that might be:

var myStr = "Product"
eval("var p = new " + myStr + "()");

But it's fraught with potential vulnerabilities, especially if the string is coming from user input.

5 Comments

try searching for "use strict" for eval
This could be much better solved with a name -> constructor function map.
@FelixKling I wish there was an example of the name->constructor function map
@slashdottir: var constructorMap = {Product: Product}; var p = new constructorMap[myStr]();
@FelixKling oic ty
1

Just another implementation:

var nameOfThang = 'Person';
var nameOfThingzName = 'The Dude';

var thangs = { Person: {name: 'Legowski'}, Cars: {} };
var person  = new (eval(thangs[nameOfThang].constructor))();
person.name = new (eval(thangs.Person.name.constructor))(nameOfThingzName) .toString();

console.log('@thang, #Person', person);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.