javascript适用于构造函数,抛出“畸形形式参数” [英] javascript apply on constructor, throwing "malformed formal parameter"
问题描述
感谢对这个问题的精彩回应我了解如何调用JavaScript函数varargs。
现在我想使用应用构造函数我在此信息上发现了一些有趣的信息。<。 / p>
但我的代码正在引发错误
尝试1:
var mid_parser = new Parser.apply(null,mid_patterns);
错误:
code> TypeError:Function.prototype.apply on incompatible [object Object]
:
attempt 1:
var mid_parser = new Parser.prototype.apply(null,mid_patterns);
错误:
code> TypeError:Function.prototype.apply on incompatible [object Object]
:
function Parser()
{
this.comparemanager = new CompareManager
}
mid_patterns = [objA,objB,objC]
var mid_parser = new Parser();
Parser.constructor.apply(mid_parser,mid_patterns);
错误:
code> syntax_model.js:91:SyntaxError:malformed正式参数
var mid_parser = Parser.apply(null,mid_patterns);
错误:
code> TypeError:this.init is undefined // init是Parser.prototype的函数
我现在有一个解决方法:
function Parser()
{
if .length)this.init.call(this,arguments); //调用init只有当参数
}
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调用构造函数?
可以使用apply并传递一个空对象作为此
参数:
var mid_parser = {};
Parser.apply(mid_parser,mid_patterns);
但是这个解决方案不会关心原型链。
您可以使用 new
运算符创建 Parser
对象,但不传递参数,然后使用 apply
重新运行构造函数:
var mid_parser = new Parser();
Parser.apply(mid_parser,mid_patterns);
thanks to wonderful responses to this question I understand how to call javascript functions with varargs.
now I'm looking to use apply with a constructor
I found some interesting information on this post.
but my code is throwing errors
attempt 1:
var mid_parser = new Parser.apply(null, mid_patterns);
error:
TypeError: Function.prototype.apply called on incompatible [object Object]
attempt 2: attempt 1:
var mid_parser = new Parser.prototype.apply(null, mid_patterns);
error:
TypeError: Function.prototype.apply called on incompatible [object Object]
attempt 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);
error:
syntax_model.js:91: SyntaxError: malformed formal parameter
attempt 3:
var mid_parser = Parser.apply(null, mid_patterns);
error :
TypeError: this.init is undefined // init is a function of Parser.prototype
I have a workaround for the time being:
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);
this works pretty well, but it's not as clean and universal as I'd like.
is it possible in javascript to call a constructor with varargs?
You could use apply and pass an empty object as the this
argument:
var mid_parser = {};
Parser.apply(mid_parser, mid_patterns);
But that solution will not take care about the prototype chain.
You could create a Parser
object, using the new
operator, but without passing arguments, and then use apply
to re-run the constructor function:
var mid_parser = new Parser();
Parser.apply(mid_parser, mid_patterns);
这篇关于javascript适用于构造函数,抛出“畸形形式参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!