0

In C# to create an new instance by name and arguments, we can use reflection as follows:

var instance = Activator.CreateInstance("Com.Account.Person", 
              new object[] { "Jack", 195, 'USA' });

How to do this in Javascript?

var clazzName = document.getElementById('clazzName'); // from an input in the page 
var args = document.getElementById('args').split(','); // from another input in the page
var instance = ....... // how?
Zach
  • 5,715
  • 12
  • 47
  • 62
  • 2
    See if this helps http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible – elclanrs Feb 23 '14 at 09:07

3 Answers3

0

You can't. JavaScript does not have classes. It has only objects.

stepanian
  • 11,373
  • 8
  • 43
  • 63
0

If you have a factory method of the form:

function MyFactoryMethod() {
  // Perform instantiation here
  return new MyObject();
}

Then you can invoke it, knowing only its name as a string, by doing:

var name = "MyFactoryMethod"
var myObj = window[name]();

That said, I don't endorse this pattern, and I have an inkling that this will get you into tricky situations down the road.

Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • Where is the instantiation ? – Royi Namir Feb 23 '14 at 09:05
  • Oops, you're right. Changed this to use a factory pattern. – Jon Newmuis Feb 23 '14 at 09:08
  • @Royi You can't have instantiation because there are no classes. See my answer. This answer will cause confusion to new JavaScript learners. – stepanian Feb 23 '14 at 09:09
  • Not having classes doesn't mean that the term "instance" is wrong. A class is an object too. – elclanrs Feb 23 '14 at 09:13
  • @elcanrs A class is not the same thing as an object. The term Instance in JavaScript is confusing, although I agree that it is used. Object is much better. A class is a blueprint. An instance is an object built from that blueprint. JavaScript creates only objects. It cannot create blueprints. – stepanian Feb 23 '14 at 09:18
  • I guess it depends on the language. An "Object" is more abstract than a "Class". In high level scripting languages a class is typically an object, think Ruby, Python, PHP... – elclanrs Feb 23 '14 at 09:28
  • @elclanrs In C# and Java, a class is nothing until it is instantiated into an object. Otherwise, it only exists as a blueprint of an object. JavaScript does not have an equivalent. Everything you do in JavaScript creates objects. Even functions are objects. – stepanian Feb 23 '14 at 09:32
  • @stepanian: Good to know. I'm not familiar with C# or Java. It seems these terms are confusing in general though. It's like trying to explain functions, scopes and closures in JavaScript... – elclanrs Feb 23 '14 at 09:36
-1

If you have for ex.

<input id="element" name="elementname" type="text" value="elementvalue" />

and want to get "elementname", "text" and "elementvalue"? You can use this:

var element = document.getElementById('element');
var name = element.name;
var type = element.type;
var value = element.value;
Al.G.
  • 4,327
  • 6
  • 31
  • 56