-1

So I was looking to get an idea working but I can't seem to figure it out. I would like like to do something like the following.

// my old fasion code but not very flexibale 
function runCode() {
  var x = new myObject();
  return x.run();
}

What I really like to be able to do.....

// pass in string paramater of object to create
// objName is the name of an object string to new like "myObject"
function runCode(objNameToUse) {
  var x = new Object(objNameToUse);
  return x.run();
}
runCode("myObject");

How could I do something like this using javascript?

  • 1
    What have you tried to do so far? – Eddie D Oct 29 '18 at 23:06
  • 1
    `Object` generally refers to the global `window.Object`. Better to use a different variable name, or you will easily cause confusion – CertainPerformance Oct 29 '18 at 23:07
  • @CertainPerformance There is no such thing as the Global `window.Object`. `Object` is the most fundamental object type in JavaScript, but that has nothing to do with Global or `window`. – Scott Marcus Oct 29 '18 at 23:08
  • @ScottMarcus `Object === window.Object` -> `true`? Hrm, I don't know, actually, if referencing `Object` would work without `window` or some other global having that property – CertainPerformance Oct 29 '18 at 23:09
  • 1
    I stand corrected, in part. The BOM doesn't define an `Object`, JavaScript does. `window.Object` simply points to that. In other words, `Object` is a native member of ECMAScript, not `window`. – Scott Marcus Oct 29 '18 at 23:10
  • @ScottMarcus: JavaScript defines it to be on the global object, which is `window` for browsers. Anyway, was just disambiguating the name in the comment. – Ry- Oct 29 '18 at 23:11
  • 1
    @Ry- The Global object is only `window` in a browser. It isn't in, say Node.js. That's why it's important to distinguish where its definition lies. – Scott Marcus Oct 29 '18 at 23:12
  • basically https://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript – epascarello Oct 29 '18 at 23:14
  • Possible duplicate of https://stackoverflow.com/questions/1366127/how-do-i-make-javascript-object-using-a-variable-string-to-define-the-class-name If the answers from the duplicates don't fully address your question please edit it to include why and flag this for re-opening. Thanks! – lucascaro Oct 30 '18 at 01:10
  • Possible duplicate of [Create object from string in JavasScript ECMAScript 6](https://stackoverflow.com/questions/31776949/create-object-from-string-in-javasscript-ecmascript-6) – Jeto Oct 30 '18 at 07:34

2 Answers2

0

You can make a mapping from names of available constructors to the constructors themselves, like with an object:

var someConstructors = Object.create(null);
someConstructors.myObject = myObject;
…

function runCode(objNameToUse) {
  var constructor = someConstructors[objNameToUse];
  var x = new constructor();
  return x.run();
}

runCode("myObject");

Object.create(null) is an object that doesn’t inherit the properties of Object.prototype, so you don’t have unexpected extra “keys”, like hasOwnProperty. Important because the usual reason to do this is if you wanted to create objects based on a string outside of your control (e.g. user input); in many other cases, you could just pass myObject directly to runCode as a function instead of a name.

So, make sure this isn’t actually what you’re looking for for “flexibility”:

function runCode(constructor) {
  var x = new constructor();
  x.run();
}

runCode(myObject);
Ry-
  • 218,210
  • 55
  • 464
  • 476
-2

If you're absolutely sure that you 100% control which class name is passed to your function at any time, you can use eval:

class myObject {
  run() {
    console.log('run');
  }
}

function runCode(objNameToUse) {
  var x = new (eval(objNameToUse))();
  return x.run();
}

runCode('myObject');
Jeto
  • 14,596
  • 2
  • 32
  • 46
  • You know I thought about the eval. I am always told eval is evil but I thought it would work for my case. However, this environment I am working in has disallowed eval. I was able to test on my personal machine and yes eval would work just can't use it for my needs. Thanks – Stephen Altemus Oct 29 '18 at 23:25
  • Yeah, it's to be avoided 99% of the time but if you're entirely in control of what you pass over to it then in some very rare occurrences it can be practical, and I thought that was one of these cases. Guess whoever downvoted disagreed :) Anyway, I'll leave the answer in case it can help future visitors, I feel like the warning is clear enough as it is. – Jeto Oct 29 '18 at 23:29
  • Just found it's a dupe of [Create object from string in JavasScript ECMAScript 6](https://stackoverflow.com/questions/31776949/create-object-from-string-in-javasscript-ecmascript-6), so I just voted to close the question. I like how one of the solutions suggests `eval` and is upvoted there, btw. – Jeto Oct 30 '18 at 07:36