Use of .apply() with 'new' operator. Is this possible?

后端 未结 30 2709
Happy的楠姐
Happy的楠姐 2020-11-22 00:39

In JavaScript, I want to create an object instance (via the new operator), but pass an arbitrary number of arguments to the constructor. Is this possible?

30条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 00:55

    This works!

    var cls = Array; //eval('Array'); dynamically
    var data = [2];
    new cls(...data);
    

提交回复
热议问题