使用参数列表在JS 1.2上创建对象

时间:2017-07-19 09:08:35

标签: javascript

我没有找到如何在史前javascript(可能是1.2)中创建对象的方法:

  • 我有参数数组。
  • 我有一个构造函数。

现在我想做点什么:

var constructor = ...;
return constructor.apply(.?., parameterList);

var constructor = ...;
return new constructor(...parameterList);

可以在javascript中进行吗?我找到了一个正在创建重复对象的黑客。

var constructor = ...;
var instance = new constructor;
parameterList.length > 0 ? constructor.apply(instance, parameterList) : null;
return instance;

1 个答案:

答案 0 :(得分:0)

将解决方案从问题转移到答案:

  

修改1.0

     

所以我找到了非常有趣的解决方案:

     
private List<WebElement> table = driver.findElements(By.xpath("//div[contains(@class,'hdrInv')]"));
        System.out.print("No of Tables: "+table.size());
        for(int tabind=0; tabind<table.size(); tabind++){
            System.out.println("ROWText: "+table.get(tabind).findElement(By.xpath(".//div[@class='col']/b")).getText());
        }
     

一些缺点可能是一直显示var instance = (function (constructor, parameterList) { var creator = constructor; function Constructor() { creator.apply(this, parameterList); } Constructor.prototype = creator.prototype; return new Constructor; }(...constructor function comes here..., parameterList)); (即使可能创建了真正的类型)。

     

修改1.0.1

     

积分在这里:http://tobyho.com/2010/11/22/javascript-constructors-and/