博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 分号_为什么显式分号在JavaScript中很重要
阅读量:2522 次
发布时间:2019-05-11

本文共 3155 字,大约阅读时间需要 10 分钟。

javascript 分号

I am in "Effective JavaScript" training at 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.

— Shruti Kapoor (@shrutikapoor08)

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

-Shruti Kapoor(@ shrutikapoor08)

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

I took Effective JavaScript training by 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.

几个月前,我接受了进行的有效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 = 1const 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 —)

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

"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.

— Shruti Kapoor (@shrutikapoor08)

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

-Shruti Kapoor(@ shrutikapoor08)

翻译自:

javascript 分号

转载地址:http://jfwzd.baihongyu.com/

你可能感兴趣的文章
【tool】测试驱动开发全攻略
查看>>
VIM命令图---可定制版
查看>>
《坐热板凳》第八次团队作业:Alpha冲刺(第三天)
查看>>
关于wxWidgets
查看>>
codevs 1160 蛇形矩阵
查看>>
在outlook中查找Skype的聊天记录
查看>>
netsh命令
查看>>
nginx set变量后lua无法改值
查看>>
baseAdapter
查看>>
别让你妈知道!
查看>>
JAVA设计模式之迭代子模式
查看>>
Java程序生成exe可执行文件
查看>>
什么是blob,mysql blob大小配置介绍
查看>>
模运算的规则
查看>>
CSS样式布局入门介绍,非常详尽
查看>>
android app崩溃日志收集以及上传
查看>>
3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录
查看>>
面试记-(1)
查看>>
压力测试 相关
查看>>
android update automatically ( android 自动升级)
查看>>