简体   繁体   English

如何使用JSHint禁用有关'this'和严格模式的警告?

[英]How to disable warnings about 'this' and strict mode using JSHint?

I am writing a web app using AngularJS (v1.5) so I have some controllers, and in those controllers I am often declaring something like : 我正在使用AngularJS(v1.5)编写一个Web应用程序,所以我有一些控制器,在那些控制器中,我经常声明:

function myController($someDirectives, ...){
    var ctrl = this;
    // My code
}

The thing is when I JSHint my code, I get this warning message for all of my 'this' declared in controllers : 问题是当我JSHint我的代码时,我得到控制器中声明的所有'this'的警告消息:

If a strict mode function is executed using function invocation, its 'this' value will be undefined. 如果使用函数调用执行严格模式函数,则其“this”值将是未定义的。

I must precise that in my .jshintrc file, I set "strict":false . 我必须准确地说,在我的.jshintrc文件中,我设置了"strict":false Does anyone know how to disable this message in particular? 有谁知道如何禁用此消息?

Thanks in advance. 提前致谢。

set the configuration in .jshintrc file .jshintrc文件中设置配置

{
  "validthis": true // Tolerate using this in a non-constructor 
}

您始终可以覆盖代码块中的jshint选项 ,即。

/* jshint validthis: true */

I'm having the same issue. 我有同样的问题。 I'm doing " indirect invocation " with the function in question, not "function invocation", and 'this' is referenced many times in the function body. 我正在使用有问题的函数进行“ 间接调用 ”,而不是“函数调用”,并且在函数体中多次引用“this”。

In my case, I was having so many of these "errors" that jsHint quit before scanning my whole script. 在我的情况下,我有很多这些“错误”,jsHint在扫描整个脚本之前就退出了。

To get around this I put this at the top of my script-file: 为了解决这个问题,我把它放在脚本文件的顶部:

/*jshint maxerr: 10000 */

It did not suppress the errors, but at least it allowed me to scroll down to see jsHint's analysis of the entire script. 它没有抑制错误,但至少它允许我向下滚动以查看jsHint对整个脚本的分析。

I had the same issue, with a very similar environment angular 1.5.5 always getting the same lint warning: 我有同样的问题,非常相似的环境angular 1.5.5总是得到相同的lint警告:

If a strict mode function is executed using function invocation, its 'this' value will be undefined. 如果使用函数调用执行严格模式函数,则其“this”值将是未定义的。

I've changed the name of my component's main function starting with upper-case and the warning disappeared 我已经更改了组件主要功能的名称,从大写开始,警告消失了

function MyController($someDirectives, ...){

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

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