开发者

Dynamic instantiation in JavaScript

Apart from eval(`o = new ${className}(${args.join(", ")})`), is there any other way to instantiate an object using var开发者_C百科iable argument list?

E.g.: var foo = instantiate(className, [arg1, arg2, ...])


You can instantiate an object with a variable argument list like this:

function instantiate(className, args) {
    var o, f, c;
    c = window[className]; // get reference to class constructor function
    f = function(){}; // dummy function
    f.prototype = c.prototype; // reference same prototype
    o = new f(); // instantiate dummy function to copy prototype properties
    c.apply(o, args); // call class constructor, supplying new object as context
    o.constructor = c; // assign correct constructor (not f)
    return o;
}

Side note: you may wish to pass a direct reference to the class constructor function:

var foo = instantiate(Array, [arg1, arg2, ...]);
// Instead of:
var foo = instantiate("Array", [arg1, arg2, ...]);

... which makes this compatible with non-global functions.


Using Object.create() in ES5:

function instantiate(constructor, args) {
    var instance = Object.create(constructor.prototype);
    constructor.apply(instance, args);
    return instance;
}

Using the spread operator in ES6:

var foo = new constructor(...args);


Well you can always do as follows. Anything added to Dino prototype can be shared among the instantiated objects The difference from normal constructor pattern is, the instantiated objects do not have to have the exact same private properties set. They can be set dynamically for each one of them.

function Dino(a,b){
  for(i = 0; i< a.length; i++) this[a[i]] = b[i];
}

var props = ["foo", "bar"],
   values = [42, 37],
      obj = new Dino(props,values);
console.log(obj);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜