JShint和下面的代码行有点问题。
$location.path('map-' + map.id + '/venue-' + map.attributes.default_venue.value);
我收到错误,Identifier 'default_venue' is not in camel case.
这通常不是问题,但我无法控制变量名称 - 它是通过 JSON API 引入的。
有什么方法可以为受影响的变量或它们出现的行抑制此问题?
抱歉,如果之前有人问过这个问题,我很确定一定是这样,但我找不到解决方案。
> JSHint 在函数级别服从指令,因此您可以找到封闭函数并向其添加camelcase
选项。下面是一个示例:
/*jshint camelcase: true */
var not_camel_case = 1; // Warns
function example() {
/*jshint camelcase: false */
var not_camel_case = 2; // Does not warn
}
根据JSHint Docs,您可以在名为.jshintrc的同一目录中创建一个配置文件,或者一直到根目录的任何目录。我只是用这个设置我的:
{
"camelcase": false
}
这里还有很多其他选项:http://jshint.com/docs/options/#camelcase
我将来自 api 的属性名称放在一个单独的字符串中。 例如:
var defaultVenueAttributeKey = 'default_venue';
$location.path('map-' + map.id + '/venue-' + map.attributes[defaultVenueAttributeKey].value);
它有点冗长,但您可以将来自 API 的所有属性名称组合在一起,然后它使响应 API 更改更容易。
接受的答案/*jshint camelcase: true */
对我不起作用。我仍然收到错误。
我查看了文档,发现这个解决方案对我有用:
/*eslint camelcase: ["error", {properties: "never"}]*/
试试这样的东西。虽然邪恶,但它会起作用。
var foo;
$.each( jsonArray, function ( i, value ) {
if ( i === 'array_element' ) {
foo = value;
}
});