简体   繁体   English

使用“使用严格”,但仍会收到警告

[英]Using “use strict” but still get the warning

HI guys Im trying to get use to using JSLint and I am getting the 'use strict' warning and after adding it to the function I still get the missing 'use strict' statement 嗨,大家好,我试图习惯于使用JSLint,并且收到“使用严格”警告,将其添加到函数中后,我仍然收到缺少的“使用严格”语句

    fs.readFile('file', 'utf8', function (err, data) {
    if (err) { "use strict"; throw err; }
    var obj = JSON.parse(data);
  });

i was curious to what i am doing wrong? 我很好奇我做错了什么?

I'm not 100% positive on this, but I've only ever used 'use strict'; 我对此不是100%正面的,但是我只使用过'use strict'; at the top of a given .js file, not embedded in the code. 在给定.js文件的顶部,而不是嵌入在代码中。 And I've not gotten the warning after doing so. 而且这样做之后我还没有得到警告。 Perhaps that's where jslint looks for it. 也许这就是jslint寻找它的地方。

Further, looking at the documentation for it, use strict should be scoped either to the whole script, (top of the file) or to a given function (top of the function, right after the opening brace). 此外,在查看相关文档的过程中, use strict应该限制​​在整个脚本(文件顶部)或给定函数(函数顶部,右括号后)的范围内。 So regardless of jslint's capabilities, that should be fixed. 因此,不管jslint的功能如何,都应该将其修复。

The location of the "use strict" directive is the issue. 问题是"use strict"指令的位置。

Usages: 用途:

// Top of script file
"use strict";
/* ... code ... */

Or: 要么:

function b() {
  "use strict";
  /* ... code ... */
}

You're using it inside the block of an if branch. 您正在if分支的块内使用它。

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

相关问题 使用webpack和jshint-loader时如何摆脱使用限制警告? - How to get rid of use-strict warning when using webpack and jshint-loader? 更改为Bluebird Promise,但仍然使用猫鼬得到过时的警告,为什么? - Changed to Bluebird Promise, but still get deprecated warning using mongoose, why? 禁用“使用use strict的函数形式”但保留“Missing'use strict'statement”警告 - Disable “use the function form of use strict” but keep the “Missing 'use strict' statement” warning 使用“use strict”和变量范围 - using “use strict” and variable scope 在ES6中,“严格使用”是否仍以相同方式工作? - Does “use strict” still work the same way in ES6? JavaScript:可以使用单引号('use strict')启用ECMAScript 5的严格模式(“use strict”)吗? - JavaScript: Can ECMAScript 5's Strict Mode (“use strict”) be enabled using single quotes ('use strict')? Chrome网上应用店过于严格警告使用公共API - Overly strict warning from Chrome Web Store for using a public API 使用电子商店时来自 ajv 的严格模式警告 - A strict mode warning from ajv when using electron-store 在严格模式下使用 UNSAFE_componentWillReceiveProps 的反应警告 - React warning for Using UNSAFE_componentWillReceiveProps in strict mode 如何使用严格模式而不在 JSlint 中出错 - How to use strict mode and not get an error in JSlint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM