分号 | Semicolons
Semicolons
- No semicolons. (see: 1, 2, 3)
eslint:
semi
window.alert('hi') // ✓ ok - window.alert('hi'); // ✗ avoid
- Never start a line with
(
,[
,`
, or a handful of other unlikely possibilities. This is the only gotcha with omitting semicolons, andstandard
protects you from this potential issue. (The full list is:[
,(
,`
,+
,*
,/
,-
,,
,.
, but most of these will never appear at the start of a line in real code.) eslint:no-unexpected-multiline
// ✓ ok;(function () { window.alert('ok')}()) // ✗ avoid(function () { window.alert('ok')}()) // ✓ ok;[1, 2, 3].forEach(bar) // ✗ avoid[1, 2, 3].forEach(bar) // ✓ ok;`hello`.indexOf('o') // ✗ avoid`hello`.indexOf('o') Note: If you're often writing code like this, you may be trying to be too clever. Clever short-hands are discouraged, in favor of clear and readable expressions, whenever possible. Instead of this: ;[1, 2, 3].forEach(bar) This is strongly preferred: var nums = [1, 2, 3]nums.forEach(bar)
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com