javascript 分号_为什么显式分号在JavaScript中很重要

javascript 分号

I am in "Effective JavaScript" training at @PayPalEng by Douglas Crockford and cannot express what an enlightening experience it has been! I realized today why using explicit semi-colons is so important in JS. Will share my insights soon. #javascript #webdevelopment #PayPal

— Shruti Kapoor (@shrutikapoor08)

我正在接受Douglas Crockford在@PayPalEng进行的“有效JavaScript”培训,并且无法表达出这种启发性的体验! 今天我意识到为什么在JS中使用显式分号如此重要。 即将分享我的见解。 #javascript #webdevelopment #PayPal

-Shruti Kapoor(@ shrutikapoor08)

November 28, 2018

十一月28,2018

自动分号插入可能导致错误的陷阱 (Gotchas where automatic semicolon insertion can lead to bugs)

I took Effective JavaScript training by Douglas Crockford a few months ago. One thing that stuck with me since then is the importance of using explicit semicolons in JavaScript. For a while, I have been lazily avoiding writing the ; and assuming the parser will do my job correctly for me. In this post, I want to present some examples that changed my mindset.

几个月前,我接受了Douglas Crockford进行的有效JavaScript培训。 从那时起,我一直困扰着我的一件事就是在JavaScript中使用显式分号的重要性。 一段时间以来,我一直在懒惰地避免编写; 并假设解析器将为我正确完成我的工作。 在这篇文章中,我想提出一些改变心态的例子。

例子1 (Example 1)

What do you expect the output of this to be?

您期望它的输出是什么?

const test = () => {
 return 
 {
  ok : true
 }
}
console.log(test())

You would expect the output of this to be an object with a property ok set to true. But instead, the output is undefined. This is so because since the curly brace starts on a new line, automatic semicolon completion changes the above code to this:

您可能希望它的输出是一个object ,其属性ok设置为true 。 但是,输出是undefined 。 之所以这样,是因为因为花括号在新行开始,所以自动分号完成将上面的代码更改为:

const test = () => {
 return;
 {
  ok : true
 }
}

Fix: Use curly braces on the right of return and explicit semicolons:

修复 :在return和显式分号右边使用花括号:

const test = () => {
 return {
  ok : true
 }
};

例子2 (Example 2)

const a = 1
const b = 2
(a+b).toString()

What do you think happens in the above code? We get an error Uncaught ReferenceError: b is not defined. This is because the parenthesis on the third line is interpreted as a function argument. This code is converted to this:

您认为上述代码中发生了什么? 我们收到错误信息Uncaught ReferenceError: b is not defined. 这是因为第三行上的括号被解释为函数参数。 这段代码被转换为:

const a = 1;
const b = 2(a+b).toString();

In the circumstance that an assignment statement must begin with a left parenthesis, it is a good idea for the programmer to provide an explicit semicolon at the end of the preceding statement rather than to rely on automatic semicolon insertion.

在赋值语句必须以左括号开头的情况下,对于程序员来说,在前一条语句的末尾提供显式分号而不是依靠自动分号插入是一个好主意。

In the circumstance that an assignment statement must begin with a left parenthesis, it is a good idea for the programmer to provide an explicit semicolon at the end of the preceding statement rather than to rely on automatic semicolon insertion.

在赋值语句必须以左括号开头的情况下,对于程序员来说,在前一条语句的末尾提供显式分号而不是依靠自动分号插入是一个好主意。

I have learned to be careful when using automatic semi-colon insertion.

我学会了使用自动分号插入时要小心。

进一步阅读— (Further Reading —)

  1. Automatic semicolon insertion rules

    自动分号插入规则

  2. Blog post by Bradley Braithwaite inspired by the same lecture

    Bradley Braithwaite的博客文章受同一演讲的启发

你学到新东西了吗? 有意见吗? 知道开发笑话吗? 鸣叫我@ shrutikapoor08 (Did you learn something new? Have comments? Know a DevJoke? Tweet me @shrutikapoor08)

"I always tell women: when you get to the top, get back in the elevator and bring a woman up with you" - Eunice Kennedy Shriver. Words of wisdom. #fempire #womenintech #womenleaders

— Shruti Kapoor (@shrutikapoor08)

“我总是告诉女人:当你到达山顶时,回到电梯里,然后把一个女人带到你身边。”-Eunice Kennedy Shriver。 至理名言。 帝国 妇女 科技 妇女 领袖

-Shruti Kapoor(@ shrutikapoor08)

January 17, 2019

一月17,2019

翻译自: https://www.freecodecamp.org/news/codebyte-why-are-explicit-semicolons-important-in-javascript-49550bea0b82/

javascript 分号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值