【发布时间】:2013-06-14 17:49:23
【问题描述】:
JSLint 坚持认为 .call 的使用有问题:
function GridView(tableArray, tableId, multiselect) {
"use strict";
if (multiselect == undefined) {
this.multiselect = false;
} else {
this.multiselect = multiselect;
}
this.tableID = tableId;
this.propertiesArr = [];
this.tableHTML = undefined;
this.oTable = undefined;
this._constructTable.call(this, tableArray);
}
错了。好吧,无论如何,出乎意料。我一生都无法弄清楚为什么,代码有问题吗?它似乎有效,但我担心意外行为。
【问题讨论】:
-
如果
_constructTable已经在实例上可用(大概是通过GridView.prototype),为什么还要使用.call?你就不能this._constructTable(tableArray);吗? -
@James Allardice:我没有,是的,我可以。我现在主要是在尝试“通话”。那么 JSLint 是否足够聪明,可以看出代码是愚蠢的?
-
似乎是,是的。如果您从
_constructTable之前删除this.,或将this更改为.call中的任何其他内容,JSLint 将不再发出警告。 -
@James Allardice:嗯,这让我放心,我认为 .call 有一些非常错误的地方,我只是不知道。谢谢。
-
@JamesAllardice 可能应该将其发布为答案,因为他是对的。将“this”作为
call的上下文发送通常是出乎意料的,这就是它令人窒息的原因。 ;^)