简体   繁体   English

jshint根据函数是否无效给出“缺少分号”?

[英]jshint gives “missing semicolon” or not based on whether the function is void?

I am using jshint in VSCode. 我在VSCode中使用jshint。 jshint gives a "missing semicolon" warning at the end '}' of the function below jshint在以下函数的末尾“}”处给出“缺少分号”警告

void function doSomething(){
    console.log('Hello, World');
}

在此处输入图片说明

And adding the semicolon satisfies jshint: 并添加分号满足jshint:

在此处输入图片说明

But after I remove void , the warning goes away without the semicolon: 但是,在我删除void ,警告消失了,但没有分号:

void function doSomething(){
    console.log('Hello, World');
}

在此处输入图片说明

What is the logic behind this? 这背后的逻辑是什么? More generally, is there official style guidance such as PEP8 in Python for Javascript addressing the best practice for semicolon? 更一般而言,是否有官方的样式指南,例如Python中用于Javascript的PEP8,解决了分号的最佳做法?

With the linting rules you have: 使用棉绒规则,您可以:

  • Function declarations do not need to be followed by a semi-colon. 函数声明不需要后跟分号。
  • Expressions, including those which include a function expression, do. 表达式(包括包含函数表达式的表达式)都可以。

By putting the void operator before the function keyword, you force it into expression context. 通过将void运算符放在function关键字之前,可以将其强制进入表达式上下文。

Since it is in expression context, you do nothing with the function in the expression, and you void the result, it becomes pointless. 由于它在表达式上下文中,因此您对表达式中的函数不执行任何操作,并且使结果无效,结果变得毫无意义。 The expression doesn't do anything at all. 该表达式根本不执行任何操作。 It doesn't even create a variable with the function stored in it, which is why this errors: 它甚至不使用存储在其中的函数创建变量,这就是为什么会出错:

 void function x() { console.log(1) }; x(); 

More generally, what's the best practice rule on semicolns in javascript? 更一般而言,javascript中分号的最佳实践规则是什么?

A highly opinionated subject. 一份非常固执己见的主题。

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

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