Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
Egil Hansen Avatar asked Mar 23 '10 14:03

Egil Hansen


2 Answers

To expand on Roland Illig's answer:

In non-strict mode, this is bound to the global scope when it isn't bound to anything else. In strict mode it is undefined. That makes it an error to use it outside of a method.

like image 64
Jørgen Fogh Avatar answered Nov 03 '22 07:11

Jørgen Fogh


I had a look at the source code of jslint, which says:

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.

like image 27
Roland Illig Avatar answered Nov 03 '22 08:11

Roland Illig