【问题标题】:jslint: why does this code result in a "Strict violation" error message?jslint:为什么此代码会导致“严格违反”错误消息?
【发布时间】:2011-01-30 19:50:19
【问题描述】:

运行以下简单代码会导致“严格违规”。错误信息。我一直在尝试查找有关原因以及如何解决它的文档。任何意见将不胜感激。

错误:

Error:

Problem at line 6 character 4: Strict violation.

} (this));

示例代码:

/*jslint browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */

"use strict";

(function (window) {
} (this));

问候,埃吉尔。

【问题讨论】:

标签: javascript jslint


【解决方案1】:

扩展 Roland Illig 的回答:

在非严格模式下,this 在未绑定到其他任何内容时绑定到全局范围。在严格模式下,它是未定义的。这使得在方法之外使用它是错误的。

【讨论】:

  • this 如果在全局上下文中使用,仍然指全局范围,但在严格模式下的方法中不指代。
【解决方案2】:

我看了一下jslint的源码,上面写着:

function reservevar(s, v) {
    return reserve(s, function () {
        if (this.id === 'this' || this.id === 'arguments' ||
                this.id === 'eval') {
            if (strict_mode && funct['(global)']) {
                warning("Strict violation.", this);
            } else if (option.safe) {
                warning("ADsafe violation.", this);
            }
        }
        return this;
    });
}

我猜 jslint 确实抱怨您在全局上下文中使用 this

【讨论】:

    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2019-08-31
    • 1970-01-01
    • 2023-04-02
    • 2011-03-16
    相关资源
    最近更新 更多