javascript 忽略 JSHint 中的驼峰式变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19448446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:37:09  来源:igfitidea点击:

Ignore camelcase variable in JSHint

javascriptcoding-stylejshintcode-cleanup

提问by Sam Beckham

Having a bit of an issue with JShint and the following line of code.

JShint 和以下代码行有点问题。

$location.path('map-' + map.id + '/venue-' + map.attributes.default_venue.value);

I'm getting the error, Identifier 'default_venue' is not in camel case.This wouldn't be a problem normally but I don't have any control over the variable name - it's brought in via a JSON API.

我收到错误消息,Identifier 'default_venue' is not in camel case.这通常不是问题,但我无法控制变量名称 - 它是通过 JSON API 引入的。

Is there any way I could suppress this issue for either the affected variables or on the lines in which they appear?

有什么方法可以为受影响的变量或它们出现的行抑制这个问题?

Apologies if this has been asked before, I'm pretty sure it must have been but I can't find a solution.

抱歉,如果之前有人问过这个问题,我很确定肯定是这样,但我找不到解决方案。

回答by James Allardice

JSHint obeys directives at a function level, so you can find the enclosing function and add a camelcaseoption to it. Here's an example:

JSHint 在函数级别遵守指令,因此您可以找到封闭的函数并向其添加camelcase选项。下面是一个例子:

/*jshint camelcase: true */

var not_camel_case = 1; // Warns

function example() {
  /*jshint camelcase: false */
  var not_camel_case = 2; // Does not warn
}

回答by ryanabooth

According to the JSHint Docs, you can make a config file in the same directory called .jshintrc, or any directory all the way to your root directory. I just set mine using this:

根据JSHint Docs,您可以在名为.jshintrc的同一目录或任何目录中创建一个配置文件,一直到您的根目录。我只是使用这个设置我的:

  {
    "camelcase": false
  }

There are loads of other options here: http://jshint.com/docs/options/#camelcase

这里有很多其他选项:http: //jshint.com/docs/options/#camelcase

回答by sam

I put the name of the property coming from the api in a separate string. E.g.:

我将来自 api 的属性名称放在一个单独的字符串中。例如:

var defaultVenueAttributeKey = 'default_venue';
$location.path('map-' + map.id + '/venue-' + map.attributes[defaultVenueAttributeKey].value);

It's a bit more verbose but you could group all property names coming from your API together and then it makes responding to the API changing easier.

它有点冗长,但您可以将来自 API 的所有属性名称组合在一起,然后它可以更轻松地响应 API 更改。

回答by Aryeh Beitz

The accepted answer /*jshint camelcase: true */didn't work for me. I was still getting the errors.

I looks at the docsand found this solution that worked for me:

接受的答案/*jshint camelcase: true */对我不起作用。我仍然收到错误。

我查看了文档并找到了对我有用的解决方案:

/*eslint camelcase: ["error", {properties: "never"}]*/

回答by inchikutty

Try out something like this.. Though wicked, it will work.

尝试这样的事情.. 虽然邪恶,但它会起作用。

var foo;
$.each( jsonArray, function ( i, value ) {
    if ( i === 'array_element' ) {
        foo = value;
    }
});