javascript适用于构造函数,抛出“格式错误的形式参数”

时间:2009-12-24 18:04:09

标签: javascript constructor variadic-functions

感谢对this question的精彩回复,我了解如何使用varargs调用javascript函数。

现在我想使用使用构造函数

我发现了一些有趣的信息on this post

但我的代码却抛出了错误

尝试1:

var mid_parser = new Parser.apply(null, mid_patterns);

错误:

TypeError: Function.prototype.apply called on incompatible [object Object]

尝试2: 尝试1:

var mid_parser = new Parser.prototype.apply(null, mid_patterns);

错误:

TypeError: Function.prototype.apply called on incompatible [object Object]

尝试2:

function Parser()
{
    this.comparemanager = new CompareManager(arguments);
}

mid_patterns = [objA,objB,objC]
var mid_parser = new Parser();
Parser.constructor.apply(mid_parser, mid_patterns);

错误:

syntax_model.js:91: SyntaxError: malformed formal parameter

尝试3:

var mid_parser = Parser.apply(null, mid_patterns);

错误:

TypeError: this.init is undefined // init is a function of Parser.prototype

我暂时有一个解决方法:

function Parser()
{
    if(arguments.length) this.init.call(this,arguments); // call init only if arguments
}
Parser.prototype = {
   //...
   init: function()
   {
         this.comparemanager = new CompareManager(arguments);
   }
   //...
}

var normal parser = new Parser(objA,objB,objC);

mid_patterns = [objA,objB,objC]
var dyn_parser = new Parser();
dyn_parser.init.apply(dyn_parser, mid_patterns);

这很好用,但它并不像我想的那样干净和普遍。

是否可以在javascript中使用varargs调用构造函数?

4 个答案:

答案 0 :(得分:17)

您可以使用apply并传递一个空对象作为this参数:

var mid_parser = {};
Parser.apply(mid_parser, mid_patterns);

但该解决方案不会关注原型链。

您可以使用Parser运算符创建new对象,但不传递参数,然后使用apply重新运行构造函数:

var mid_parser = new Parser();
Parser.apply(mid_parser, mid_patterns);

答案 1 :(得分:11)

更好的解决方案是创建临时构造函数,应用所需类的原型(以确保保留原型链),然后手动应用构造函数。这可以防止不必要地两次调用构造函数......

applySecond = function(){
    function tempCtor() {};
    return function(ctor, args){
        tempCtor.prototype = ctor.prototype;
        var instance = new tempCtor();
        ctor.apply(instance,args);
        return instance;
    }
}();

我测试了性能,发现这个方法在 very 简单的情况下实际上有点慢。但是,它只需要在构造函数中构造单个Date()对象,以提高效率。另外,不要忘记,如果没有传递参数,某些构造函数可能会抛出异常,因此这也更正确。

我的验证码:

var ExpensiveClass = function(arg0,arg1){
    this.arg0 = arg0;
    this.arg1 = arg1;
    this.dat = new Date();
}

var CheapClass = function(arg0,arg1){
    this.arg0 = arg0;
    this.arg1 = arg1;
}

applyFirst = function(ctor, args){
    var instance = new ctor();
    ctor.apply(instance, args);
    return instance;
}

applySecond = function(){
    function tempCtor() {};
    return function(ctor, args){
        tempCtor.prototype = ctor.prototype;
        var instance = new tempCtor();
        ctor.apply(instance,args);
        return instance;
    }
}();

console.time('first Expensive');
for(var i = 0; i < 10000; i++){
    test = applyFirst(ExpensiveClass ,['arg0','arg1']);
}
console.timeEnd('first Expensive');

console.time('second Expensive');
for(var i = 0; i < 10000; i++){
    test = applySecond(ExpensiveClass ,['arg0','arg1']);
}
console.timeEnd('second Expensive');

console.time('first Cheap');
for(var i = 0; i < 10000; i++){
    test = applyFirst(CheapClass,['arg0','arg1']);
}
console.timeEnd('first Cheap');

console.time('second Cheap');
for(var i = 0; i < 10000; i++){
    test = applySecond(CheapClass,['arg0','arg1']);
}
console.timeEnd('second Cheap');

结果:

first Expensive: 76ms
second Expensive: 66ms
first Cheap: 52ms
second Cheap: 52ms

答案 2 :(得分:7)

您可以利用apply(...)链接构造函数来实现此目的,尽管这需要创建代理类。下面的construct()功能可让您执行以下操作:

var f1 = construct(Foo, [2, 3]);
// which is more or less equivalent to
var f2 = new Foo(2, 3);

construct()功能:

function construct(klass, args) {

  function F() {
    return klass.apply(this, arguments[0]); 
  }; 

  F.prototype = klass.prototype; 

  return new F(args);

}

使用它的一些示例代码:

function Foo(a, b) {
  this.a = a; this.b = b;
}

Foo.prototype.dump = function() {
  console.log("a = ", this.a);
  console.log("b = ", this.b);
};

var f = construct(Foo, [7, 9]);

f.dump();

答案 3 :(得分:0)

要完成@CMS解决方案并保留原型链,您可以这样做:

var mid_parser = {};
mid_parser.__proto__ = Parser.prototype;
Parser.apply(mid_parser, mid_patterns);

作为旁注,它不适用于IE 8 - 。