split 逗号或分号

…that is the question.

…就是那个问题。

One of the most common JavaScript questions I am asked by students (apart from “Why??”) relates to the semicolon as a line terminator. Many websites and tutorials will briefly state that the use of semicolons in JavaScript is optional, which can leave the learner confused — should semicolons be used or not?

学生问我的最常见JavaScript问题之一(“为什么?”除外)与分号作为行终止符有关。 许多网站和教程都会简要说明JavaScript中分号的使用是可选的,这可能会使学习者感到困惑-是否应使用分号?

In this article, I’ll state my personal preference (feel free to disagree with me). But, first, where did all this semicolon business come from?

在本文中,我将陈述我的个人喜好(随意不同意我的看法)。 但是,首先,所有这些分号业务从何而来?

分号简史 (A Brief History Of The Semicolon)

The short answer: semicolons are used so that the compiler or interpreter knows where the end of the statement is. Many languages, such as JavaScript, ignore whitespace completely. In JavaScript, this line:

简短的答案:使用分号,以便编译器或解释器知道语句结尾在哪里。 许多语言(例如JavaScript)完全忽略空格。 在JavaScript中,此行:

is interpreted exactly the same as this:

的解释与此完全相同:

The use of semicolons helps to differentiate between statements. It is often seen as something that started with the C programming language; however, earlier languages such as Algol and C’s predecessor, BCPL, also used the semicolon. COBOL uses the full stop (or period, if you prefer) as a semantic terminator. The reason we have semicolons as statement separators in JavaScript today owes a lot to languages such as C and Java, and the intention of making the language non-whitespace dependent.

分号的使用有助于区分语句。 它通常被视为从C编程语言开始的东西。 但是,较早的语言(例如Algol和C的前身BCPL)也使用了分号。 COBOL使用句号(或句点,如果您愿意)作为语义终止符。 今天,我们在JavaScript中使用分号作为语句分隔符的原因很大程度上归功于C和Java之类的语言,以及使该语言不依赖空白的意图。

但是……有人告诉我它们是可选的吗? (But…I was told they are optional?)

This is where some of the confusion starts to come in. I’ve always viewed JavaScript as a language with a deep inferiority complex that desperately wants programmers to like it. As a result, features are added to try and make JavaScript more popular with the developer but often end up being the most derided and confusing.

这就是一些混乱开始出现的地方。我一直将JavaScript视为一种具有深厚自卑感的语言,迫切希望程序员喜欢它。 结果,添加了功能以尝试使JavaScript在开发人员中更受欢迎,但通常最终被嘲笑得最混乱。

Type coercion and “optional” semicolons are, to me, indications of JavaScript’s neuroses. As Douglas Crockford eloquently put it, “In JavaScript, there is a beautiful, highly expressive language that is buried under a steaming pile of good intentions and blunders.”

对我来说,类型强制和“可选”分号表示JavaScript的神经症。 正如道格拉斯·克罗克福德(Douglas Crockford)雄辩地指出的那样:“在JavaScript中,有一种优美而富有表现力的语言被埋在大量的善意和错误之中。”

“In JavaScript, there is a beautiful, highly expressive language that is buried under a steaming pile of good intentions and blunders.”

“在JavaScript中,有一种美丽的,具有高度表达能力的语言被埋在大量的善意和错误之中。”

So, back to semicolons. Strictly speaking, semicolons are not optional in JavaScript. It’s just that, if you leave them out then a feature called Automatic Semicolon Insertion, or ASI, will do it for you. Now, ASI doesn’t actually physically insert the semicolons in your code, but when the code is executed, ASI will use a pre-determined set of rules to interpret where a semicolon should have been.

因此,回到分号。 严格来说,分号在JavaScript中不是可选的。 就是这样,如果您不使用它们,则称为自动分号插入(ASI)的功能将为您完成。 现在,ASI并没有实际在代码中插入分号,但是在执行代码时,ASI将使用一组预定规则来解释分号应在的位置。

Even with ASI, there are some places where semicolons should always be used, otherwise, you can get unexpected behaviour. Here’s a case in point:

即使使用ASI,在某些地方也应始终使用分号,否则,您可能会遇到意想不到的行为。 这是一个例子:

It’s a bit of a contrived example, but what you’d expect to end up with is: a = 1, b = 2, c = 3, and then a + b would be converted to a string, “3”. Due to the lack of semicolons, however, this code will fail with an error and say that “b is not a function”. This is because ASI is interpreting the code like this:

这是一个人为的示例,但是您最终希望得到的结果是:a = 1,b = 2,c = 3,然后a + b将转换为字符串“ 3”。 但是,由于缺少分号,该代码将失败并显示错误,并指出“ b不是函数”。 这是因为ASI会这样解释代码:

Again, as I said, it’s heavily contrived, but it shows how ASI can trip you up if you don’t follow the rules.

再次,正如我说的那样,它是人为设计的,但是它显示了如果您不遵守规则,ASI将如何使您绊倒。

那么,我是否应该使用它们? (So, should I use them or not?)

The easy answer is that it’s entirely up to you. But, since you’ve persevered to the end of this article, it’s only fair that I give you my opinion. And, that opinion is “Yes. Learn the rules and use semicolons in your code.”

简单的答案是,这完全取决于您。 但是,由于您一直坚持到本文结尾,所以我给您我的意见是公平的。 而且,该意见是“是的。 了解规则并在代码中使用分号。”

…since you’ve persevered to the end of this article, it’s only fair that I give you my opinion. And, that opinion is “Yes. Learn the rules and use semicolons in your code.”

…由于您一直坚持到本文的结尾,因此请您发表我的看法是公平的。 而且,该意见是“是的。 了解规则并在代码中使用分号。”

The reason I say this, and encourage my students to do the same, is that I like code to be predictable. I don’t like edge cases. Enough issues can arise in coding as it is, so eliminating areas of uncertainty is important to me. If you follow the semicolon rules, then you’re not going to get into trouble if ASI doesn’t work as you expect.

我之所以这样说,并鼓励我的学生也这样做,是因为我喜欢代码是可预测的。 我不喜欢这种情况。 照原样编码可能会出现很多问题,因此消除不确定性区域对我很重要。 如果您遵循分号规则,那么即使ASI不能按预期工作,也不会给您带来麻烦。

You don’t need to agree with me, and I’d be happy to hear opposing opinions, but whatever you decide, just be consistent. For me, to return to the title of this piece and entirely misquote Shakespeare, ’tis nobler in mind to suffer the slings and arrows of outrageous JavaScript rather than to take arms against a sea of semicolons and by ASI end them.

您无需同意我的意见,我很乐意听到反对意见,但是无论您做出什么决定,只要保持一致即可。 对我来说,要回到本文的标题并完全误引用莎士比亚的话,请记住要忍受残酷JavaScript的攻击,而不是屈服于分号之海,而在ASI的帮助下结束它们。

翻译自: https://levelup.gitconnected.com/to-semicolon-or-not-to-semicolon-a2e337827bcb

split 逗号或分号


http://www.taodudu.cc/news/show-2505518.html

相关文章:

  • python语句分号_你知道分号在各种编程语言中的作用吗?
  • javaScript中什么时候用分号;
  • python分号_在Python中拆分分号分隔的字符串
  • java分号_java – 为什么这些分号不会产生错误?
  • python分号_python中的分号(“;”)
  • C++分号
  • ThinkPad E430 选择U盘启动后又跳回选择画面
  • Thinkpad E430 vt功能开启
  • android联想搜索不到wifi,联想笔记本ThinkPad E430 无法搜索到无线网络的解决办法...
  • 联想thinkpad e430 设置u盘启动
  • ThinkPad E430光驱面板拆卸方法
  • 联想Thinkpad E430搜不到无线网络
  • 联想thinkpad E430c 重装系统之后无线连接显示红叉解决办法
  • 联想thinkpad E430C硬盘位换为固态,硬盘放于光驱位(win7+win10+ubuntu三系统安装教程)
  • Thinkpad E430c 无线开关
  • Thinkpad E430c安装Ubuntu14.04第三方驱动的选择
  • ThinkPad E430 蓝牙驱动 BCM43142A0
  • 笔记本ThinkPad E430c加装内存和SSD固态硬盘
  • e430c更换光盘托架_如何处理PC上未使用的光盘驱动器托架
  • linux系统有线网卡,Thinkpad E430+CentOS 6.4+ linux-3.10.12内核网卡驱动(无线+有线)配置...
  • Thinkpad E430c 16GB内存安装成功
  • 联想笔记本linux无线网卡,科学网—配置lenovo E430 + Ubuntu 13.04无线网卡 - 彭友松的博文...
  • Thinkpad E430C 跳过电池检测更新bios
  • Thinkpad E430 移除网卡白名单
  • 黑苹果E430c, 安装过程
  • e430c参数 thinkpad_联想ThinkPad E430c(33651E1)
  • e430c参数 thinkpad,联想ThinkPad E430c的详细参数
  • Thinkpad E430c使用u盘安装系统
  • thinkpad E430拆装与升级
  • E430 加装固态硬盘(SSD)参考

split 逗号或分号_分号或不分号相关推荐

  1. javascript 分号_让我们谈谈JavaScript中的分号

    javascript 分号 要使用它们,还是不使用它们- (To use them, or not to use them-) Semicolons in JavaScript divide the ...

  2. jquery中如何以逗号分割字符串_百度知道

    jquery中如何以逗号分割字符串_百度知道javascript本身就是带split方法的 定义和用法 split() 方法用于把一个字符串分割成字符串数组. 语法 stringObject.spli ...

  3. java if 分号_java-'if'语句结尾的分号

    java-'if'语句结尾的分号 今天,在搜索错误半小时之后,我发现可以在if语句后加上分号而不是代码,如下所示: if(a == b); // Do stuff 这基本上意味着无论a是否等于b,都可 ...

  4. java split 逗号_咦,Java拆分个字符串都这么讲究

    提到 Java 拆分字符串,我猜你十有八九会撂下一句狠话,"这有什么难的,直接上 String 类的 split() 方法不就拉到了!"假如你真的这么觉得,那可要注意了,事情远没这 ...

  5. mysql 代替分号_除了使用分号(;)终止符之外,还有其他可执行MySQL查询的内置命令吗?...

    借助以下内置命令,即使不使用分号(;)终止符,MySQL也可以执行查询. 自我 我们可以通过使用\ G选项来使用此命令.这意味着将当前语句发送到要执行的服务器,并以垂直格式显示结果.当我们使用\ G并 ...

  6. 关键词之间用分号_如何选择关键词?快看这几个要点

    论文关键词,是从论文的题名.摘要和正文中选取出来的,是对表述论文的中心内容有实质意义的词汇.一般一篇论文需要有3--8个关键词,多个关键词之间用分号分隔,按关键词的范围层次从大到小排列. 关键词的一般 ...

  7. mysql 代替分号_php – mysql FIND_IN_SET()用分号代替逗号

    我有一个看起来像这样的数据库集 Table: Notes nid | forDepts -------------- 1 | 1;2;4 2 | 4;5 Table: Positions id | n ...

  8. python语句分号_【判断题】Python程序中每条语句以分号结尾。 A. 正确 B. 错误

    [判断题]Python程序中每条语句以分号结尾. A. 正确 B. 错误 更多相关问题 当天游览出发前,导游员至少应提前()到达约定的出发地点等候游客. A. 5分钟B. 10分钟C. 20分 当导游 ...

  9. js replace 中文分号_在JS中用Replace 全部替换字符

    类型:电子教程大小:3.3M语言:中文 评分:2.8 标签: 立即下载 在JS中常用的替换字符串是用 Replace函数,我们先来看一下 Replace函数的用法 replace 方法 返回根据正则表 ...

  10. python分号_为什么在此python代码段中允许使用分号?

    HUX布斯 http://docs.python.org/reference/compound_stmts.html复合语句由一个或多个"子句"组成.子句由标题和"套件& ...

最新文章

  1. x99芯片组服务器版叫什么,Intel X99主板、Z97主板以及H97主板的区别是什么?
  2. Eureka客户端使用IP注册preferIpAddress和ip-address的区别
  3. Vue 学习第八天
  4. MySQL高级 - 存储引擎 - 选择原则
  5. 人工智能风险分析技术研究进展
  6. 利用matlab点云工具处理点云
  7. 股票软件开发中全推与点播的区别(自己留作记录的,请csdn的小编别乱删我的文章)...
  8. Scala的sealed关键字
  9. web前端之JavaScript高级程序设计六:事件
  10. python实现matlab_python 实现matlab的mapminmax方法
  11. 可控硅型号怎样识别_可控硅的鉴别
  12. SSL P2133 腾讯大战360 题目
  13. iOS m3u8本地缓存播放(控制下载并发、暂停恢复)
  14. 如何通过市场中性策略获利
  15. Linux加密和安全篇(一)gpg、对称和非对称加密、哈希算法
  16. QTextEdit 控件的妙用(以及与QPlainTextEdit的区别)
  17. 讲的真详细!花三分钟看完这篇文章你就懂了
  18. [转]兰迪.波许的最后讲座:真正实现你童年的梦想(中英文对照)12
  19. Netty-编码和解码
  20. 商品亲和性分析与关联规则挖掘

热门文章

  1. 路由器装linux系统,怎样在路由器上安装Linux / DD-WRT固件
  2. 网络安全等级保护的过程
  3. Golang 1.16 新特性-embed 包及其使用
  4. 短信下发的成功失败概率是多少?如何提高到达率?
  5. 魏吉英:IPONE5越狱
  6. sqrt方法复杂度探讨
  7. lpx寒假作业案例5
  8. 【经典详解】<T> T 和 T的用法和区别,public <T> List<T> f(T a){}的详解
  9. ssm银行账户管理系统毕业设计(附源码、运行环境)
  10. 【Derivation】 条件数学期望公式泊松分布推导(Poisson distribution)