简体   繁体   中英

Object instantiation using eval in JavaScript

Short question, if this works (and it does):

eval("new " + generator.className + "(" + generator.constructorArgs.join(", ") + ")");  

why doesn't this work:

eval(generator.className + ".prototype.constructor.apply({}, generator.constructorArgs);");

The second expression always returns undefined, but in my opinion it should work. I tried it on dummy objects like:

var dummy = function () {};

Also, is there any way I can avoid using eval in this situation?

Thanks,
Alex

Well, I think the problem is that your constructor function is not returning anything.

When you use the new operator, if the constructor function, does not return an object, the this value, which is the newly created object is returned implicitly, for example:

function Foo () {
  this.foo = 'bar';
}

new Foo(); // { foo: 'bar' }

If you invoke the function with call/apply, it will yield undefined , since there is no return value at all:

Foo.call({}); // undefined

So, the solution would be to return the this value on your constructor, eg:

function Bar() {
  this.bar =  'baz';
  //..
  return this;
}

Bar.call({}); // { bar: 'baz' }

Also, remember that using the new operator is not completely equivalent to apply a function using a new object as the this value, because when you use the new operator, the newly created object will inherit from its constructor's prototype, eg:

new Bar() instanceof Bar; // true
Bar.call({}) instanceof Bar; // false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM