jslint:为什么此代码会导致"严重违规"错误消息?

Egi*_*sen 14 javascript jslint

运行以下简单代码会导致"严格违规".错误信息.我一直在努力寻找有关原因以及如何解决问题的文档.任何输入将非常感激.

错误:

Error:

Problem at line 6 character 4: Strict violation.

} (this));
Run Code Online (Sandbox Code Playgroud)

示例代码:

/*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));
Run Code Online (Sandbox Code Playgroud)

此致,埃吉尔.

Jør*_*ogh 17

扩展Roland Illig的答案:

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


Rol*_*lig 8

我看了一下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;
    });
}
Run Code Online (Sandbox Code Playgroud)

我猜jslint真的抱怨你this在全球范围内使用.