<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>KUNKKA &#187; Semicolon Insertion</title>
	<atom:link href="http://www.kunkka.cn/tag/semicolon-insertion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kunkka.cn</link>
	<description>活着就是折腾</description>
	<lastBuildDate>Tue, 01 Jun 2010 07:38:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>浅谈javascript的分号</title>
		<link>http://www.kunkka.cn/2009/12/03/javascript-automatic-semicolon-insertion/</link>
		<comments>http://www.kunkka.cn/2009/12/03/javascript-automatic-semicolon-insertion/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 13:39:05 +0000</pubDate>
		<dc:creator>kunkka</dc:creator>
				<category><![CDATA[javascript / Dom]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Automatic]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Semicolon Insertion]]></category>

		<guid isPermaLink="false">http://www.kunkka.cn:81/?p=195</guid>
		<description><![CDATA[javascript的分号代表语句的结束符，但由于javascript具有分号自动插入规则，所以它是一个十分容易让人模糊的东西，在一般情况下，一个换行就会产生一个分号，但实际情况却不然，也就是说在javascript中换行可能产生分号，也可能不产生，是否自动插入分号，主要看上下行。所以即使是经验丰富的程序员，有时候也会头大。
在ECMAScript中对分号自动插入规则也有相应的解释：空语句，变量语句，表达式语句，do-while 语句，continue 语句，break 语句，return 语句，以及 throw 语句，这些确定的ECMAScript语句必须以分号结束。这些分号可以总是明确地出现在源代码文本中。为方便起见，在特定的情况下，源代码文本中的这些分号可以被省略。也就是说这些语句的结尾是不需要硬性的输入分号结尾，javascript会自动插入语句结束处。



想了解详细的ECMAScript分号自动插入规则可以查看以下链接：


原文
中文翻译



实践出真理，看一下下面这些例子，就明白，分号自动插入不是那么好让人捉摸的。稍微不注意就会让你头大。
return引起的血案

function test&#40;&#41;&#123;
   var a = 1;
   var b = 2;
   return //会自动插入分号
       &#40;
         a+b
       &#41;
&#125;;
alert&#40;test&#40;&#41;&#41;;

一个返回a+b值的函数，初看没任何问题，但运行alert的结果却是undefined。根据分号的自动插入规则，return 语句后面如果有换行就会自动插入分号，没有返回值也就比较好理解了。如果需要换行的话，可以这样：

function test&#40;&#41;&#123;
   var a = 1;
 [...]]]></description>
			<content:encoded><![CDATA[<p>javascript的分号代表语句的结束符，但由于javascript具有分号自动插入规则，所以它是一个十分容易让人模糊的东西，在一般情况下，一个换行就会产生一个分号，但实际情况却不然，也就是说在javascript中换行可能产生分号，也可能不产生，是否自动插入分号，主要看上下行。所以即使是经验丰富的程序员，有时候也会头大。</p>
<p>在<a href="http://bclary.com/2004/11/07/" title="查看ECMAScript文档">ECMAScript</a>中对分号自动插入规则也有相应的解释：<span style="text-decoration: underline; ">空语句，变量语句，表达式语句，do-while 语句，continue 语句，break 语句，return 语句，以及 throw 语句，这些确定的ECMAScript语句必须以分号结束。这些分号可以总是明确地出现在源代码文本中。为方便起见，在特定的情况下，源代码文本中的这些分号可以被省略。</span>也就是说这些语句的结尾是不需要硬性的输入分号结尾，javascript会自动插入语句结束处。
</p>
<p><span id="more-195"></span></p>
<dl>
<dt>想了解详细的ECMAScript分号自动插入规则可以查看以下链接：</dt>
<dd>
<ul>
<li><a href="http://bclary.com/2004/11/07/#a-7.9" title="查看详细">原文</a></li>
<li><a href="http://let-in.blogspot.com/2007/05/ecma-26279-automatic-semicolon.html" title="查看详细">中文翻译</a></li>
</ul>
</dd>
</dl>
<p>实践出真理，看一下下面这些例子，就明白，分号自动插入不是那么好让人捉摸的。稍微不注意就会让你头大。</p>
<p><b>return引起的血案</b></p>

<div class="wp_codebox"><table><tr id="p1955"><td class="code" id="p195code5"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #006600; font-style: italic;">//会自动插入分号</span>
       <span style="color: #009900;">&#40;</span>
         a<span style="color: #339933;">+</span>b
       <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>一个返回a+b值的函数，初看没任何问题，但运行alert的结果却是undefined。根据分号的自动插入规则，return 语句后面如果有换行就会自动插入分号，没有返回值也就比较好理解了。如果需要换行的话，可以这样：</p>

<div class="wp_codebox"><table><tr id="p1956"><td class="code" id="p195code6"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span> 
           a<span style="color: #339933;">+</span>b
          <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><b>两个闭包引起的血案</b></p>

<div class="wp_codebox"><table><tr id="p1957"><td class="code" id="p195code7"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">//不会自动插入分号</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>很诡异，解释不了，谁能告诉我～</p>
<p><b>for语句头中的两个分号，不自动插入分号</b></p>

<div class="wp_codebox"><table><tr id="p1958"><td class="code" id="p195code8"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> a<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>b<span style="color: #339933;">=</span><span style="color: #CC0000;">10</span> <span style="color: #006600; font-style: italic;">//不会自动插入分号</span>
         a<span style="color: #339933;">&lt;</span>b <span style="color: #006600; font-style: italic;">//不会自动插入分号</span>
         a<span style="color: #339933;">++</span> 
   <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>ECMAScript对以上也有解释：将分号解释为空语句和在for语句中的()中不自动插入分号是特例，是不属于自动插入规则管辖的。</p>
<p>javascript虽然是弱类型语言，ECMAScript的分号自动插入规则也让人难以理解透彻。但养成良好的代码书写习惯，手动插入分号，养成习惯，就能避免这些问题，同时会在程序调试，代码的阅读上对自己对他人都有不小的帮助。</p>
<p>同时ECMAScript还给程序员一些忠告：</p>
<ul>
<li>++ 或 &#8212; 应和其操作数出现在同一行。</li>
<li>return 或 throw 语句中的表达式应和return 或 throw 出现在同一行。</li>
<li>break 或 continue 语句中的标签应和break 或 continue 出现在同一行。</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.kunkka.cn/2009/12/03/javascript-automatic-semicolon-insertion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

