Why does JSLint give strict violation error on this function?
Asked Answered
N

1

7

JSLint gives me the "strict violation" error, although I use the "this" context inside a function which hides it from the global scope.

function test() {
    "use strict";
    this.a = "b";
}

For the record, I use the built-in JSLint parser in Webstorm.

Nasya answered 21/7, 2013 at 7:28 Comment(3)
When I paste this code into JSLint.com, all default options, I do not receive an error. What was the context of the error where changing the function name removed it?Henricks
I'm using Webstorm which maybe has an outdated version of JSLintNasya
Any chance you have two functions named (the equivalent of) test (in your live code)? Then changing case would make them different.Henricks
N
10

This is because JSLint doesn't recognize your function as a constructor. By convention, you must use uppercase letters.

function Test() {
    "use strict";
    this.a = "b";
}
Nasya answered 21/7, 2013 at 7:28 Comment(1)
You might look at jshint.com. It offers more control, rather than locking you into options controlled by Crockford's opinion rather than the specification. The problem you've run into also happens with non-constructors, if you are declaring functions you're going to put on an object as methods. (For instance, if after your function test(){...} you had obj.test = test; so you ended up with a function with a proper name on the test property, but without using a named function expression because of the problems IE has with them.)Gottschalk

© 2022 - 2024 — McMap. All rights reserved.