简体   繁体   English

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

[英]jslint: why does this code result in a “Strict violation” error message?

Running the following simple code results in a "Strict violation." 运行以下简单代码会导致“严格违规”。 error message. 错误信息。 I have been trying to find documentation on why, and how to fix it. 我一直在努力寻找有关原因以及如何解决问题的文档。 Any input will be much appreciated. 任何输入将非常感激。

The error: 错误:

Error:

Problem at line 6 character 4: Strict violation.

} (this));

The sample code: 示例代码:

/*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));

Regards, Egil. 此致,埃吉尔。

To expand on Roland Illig's answer: 扩展Roland Illig的答案:

In non-strict mode, this is bound to the global scope when it isn't bound to anything else. 在非严格模式, this势必会在全球范围内,当它没有被绑定到任何东西。 In strict mode it is undefined. 在严格模式下,它是未定义的。 That makes it an error to use it outside of a method. 这使得在方法之外使用它是一个错误。

I had a look at the source code of jslint, which says: 我看了一下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;
    });
}

I guess that jslint really complains that you are using this in a global context. 我猜jslint真的抱怨你在全局范围内使用this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM