【问题标题】:JSLint insists that "Unexpected 'call'"JSLint 坚持认为“意外的‘呼叫’”
【发布时间】: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 的上下文发送通常是出乎意料的,这就是它令人窒息的原因。 ;^)

标签: call jslint


【解决方案1】:

警告的原因是以下行:

this._constructTable.call(this, tableArray);

这个结构似乎毫无意义——你在this 的上下文中调用_constructTable 方法,如果你通过正常的调用表达式调用它,它将被调用的上下文相同。 JSLint 正是这样期望的:

this._constructTable(tableArray);

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多