<?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/"
	>

<channel>
	<title>西风瘦马博客</title>
	<atom:link href="http://blog.tugai.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tugai.net</link>
	<description>关注Web前端技术</description>
	<pubDate>Sat, 24 Jul 2010 00:44:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Diffable:一个胆大创新的Web性能优化概念</title>
		<link>http://blog.tugai.net/2010/07/13/diffable/</link>
		<comments>http://blog.tugai.net/2010/07/13/diffable/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 17:36:28 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=307</guid>
		<description><![CDATA[问题：

频繁的文件修改导致每次都需要重新下载这些修改的文件。
很小的改动都会导致缓存失效。
富互联网应用（Rich internet applications）通常包含大量的静态资源文件。

方案：

首先重缓存中申请资源。
传输完成后用Deltas更新缓存版本。
将浏览器端代码和Deltas合并生成最新的JS。

好处：

配合缓存加快网页加载。
用很小的代价完成大资源文件的小改动。

Diffable是一个通过传输浏览器端缓存文件与服务器端最新文件的差异（deltas）来加快网页加载的开源项目。它能自动比较静态文件各版本之间差异，避免重复下载那些未变化的代码，达到减少带宽传输、优化网页加载的目的。
在现代网站中，每个网页包含的静态资源（JavaScript、CSS）越来越多，为了加快网页的加载速度，通常的做法是在浏览器端缓存这些静态文件（参考YSlow的优化原则第三条），如果文件没有变化，浏览器直接从缓存中读取文件，反之，就从服务器上下载最新的文件。像Javascript、HTML、CSS等静态文件可能每周都会更新，甚至更新频率更高，但每个版本之间的改动可能不大，可能只有几行或者几十行代码发生了变化，但是为了获取最新的文件，浏览器必须重新下载整个文件。比如Google Maps包含了一个300K的JavaScript文件，当这个js文件更新时（哪怕只是修改了一行代码），浏览器都需要重新下载这个300K的JavaScript文件，这种成本消耗对用户来说是昂贵的。
有没有办法让浏览器只下载需要更新的那几行代码呢？Diffable就提供了这样的一个解决方案：仅下载有变化的代码（在Diffable中称之为Deltas），未变化的代码从浏览器缓存中获得，两部分代码合并后得到最新的代码。

图一：Deltas图解


图二：Diffable工作原理详解
目前，Diffable只是一个J2EE的开源项目，需要服务器端和浏览器端共同来实现，期待有更多的服务器端语言加入到这个项目中来。
Diffable才刚刚起步，可能许多地方还有待改进，但是，这种创新的概念是值得赞赏的，它为web优化提出了一种新的思路。
有兴趣了解更多的朋友请到官方网站：http://code.google.com/p/diffable/
官方项目介绍的PPT中也包含了不少信息：http://docs.google.com/present/view?id=dhp4jpqq_58hmf8zxdk
]]></description>
			<content:encoded><![CDATA[<p><strong>问题：</strong></p>
<ul>
<li>频繁的文件修改导致每次都需要重新下载这些修改的文件。</li>
<li>很小的改动都会导致缓存失效。</li>
<li>富互联网应用（Rich internet applications）通常包含大量的静态资源文件。</li>
</ul>
<p><strong>方案：</strong></p>
<ul>
<li>首先重缓存中申请资源。</li>
<li>传输完成后用Deltas更新缓存版本。</li>
<li>将浏览器端代码和Deltas合并生成最新的JS。</li>
</ul>
<p><strong>好处：</strong></p>
<ul>
<li>配合缓存加快网页加载。</li>
<li>用很小的代价完成大资源文件的小改动。</li>
</ul>
<p><a href="http://code.google.com/p/diffable/" target="_blank">Diffable</a>是一个通过传输浏览器端缓存文件与服务器端最新文件的差异（deltas）来加快网页加载的开源项目。它能自动比较静态文件各版本之间差异，避免重复下载那些未变化的代码，达到减少带宽传输、优化网页加载的目的。<span id="more-307"></span></p>
<p>在现代网站中，每个网页包含的静态资源（JavaScript、CSS）越来越多，为了加快网页的加载速度，通常的做法是在浏览器端缓存这些静态文件（参考<a href="https://developer.yahoo.com/performance/rules.html#expires" target="_blank">YSlow的优化原则第三条</a>），如果文件没有变化，浏览器直接从缓存中读取文件，反之，就从服务器上下载最新的文件。像Javascript、HTML、CSS等静态文件可能每周都会更新，甚至更新频率更高，但每个版本之间的改动可能不大，可能只有几行或者几十行代码发生了变化，但是为了获取最新的文件，浏览器必须重新下载整个文件。比如Google Maps包含了一个300K的JavaScript文件，当这个js文件更新时（哪怕只是修改了一行代码），浏览器都需要重新下载这个300K的JavaScript文件，这种成本消耗对用户来说是昂贵的。</p>
<p>有没有办法让浏览器只下载需要更新的那几行代码呢？Diffable就提供了这样的一个解决方案：仅下载有变化的代码（在Diffable中称之为Deltas），未变化的代码从浏览器缓存中获得，两部分代码合并后得到最新的代码。</p>
<p><img src="http://blog.tugai.net/wp-content/uploads/2010/07/deltas.png" alt="deltas" title="deltas" width="600" class="alignleft size-full wp-image-310" /></p>
<div style="text-align:center">图一：Deltas图解</div>
<p><br/><br/></p>
<p><img src="http://blog.tugai.net/wp-content/uploads/2010/07/diffable_flow.png" alt="diffable_flow" title="diffable_flow" width="576" height="554" class="alignleft size-full wp-image-311" /></p>
<div style="text-align:center">图二：Diffable工作原理详解</div>
<p>目前，Diffable只是一个J2EE的开源项目，需要服务器端和浏览器端共同来实现，期待有更多的服务器端语言加入到这个项目中来。</p>
<p>Diffable才刚刚起步，可能许多地方还有待改进，但是，这种创新的概念是值得赞赏的，它为web优化提出了一种新的思路。</p>
<p>有兴趣了解更多的朋友请到官方网站：<a href="http://code.google.com/p/diffable/">http://code.google.com/p/diffable/</a></p>
<p>官方项目介绍的PPT中也包含了不少信息：<a href="http://docs.google.com/present/view?id=dhp4jpqq_58hmf8zxdk">http://docs.google.com/present/view?id=dhp4jpqq_58hmf8zxdk</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/07/13/diffable/feed/</wfw:commentRss>
		</item>
		<item>
		<title>易让人忽视的JavaScript陷阱</title>
		<link>http://blog.tugai.net/2010/06/30/javascript-trap/</link>
		<comments>http://blog.tugai.net/2010/06/30/javascript-trap/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 15:51:25 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=302</guid>
		<description><![CDATA[从1996年网景公司第一次介绍JavaScript，这门语言自今已经有了10多年的历史了，随着互联网技术的发展，JavaScript也从诞生时只完成简单的表单验证发展成互联网上非常重要的一门语言，用它开发的网络应用已经五花八门，许多产品的功能已经接近了桌面产品。尽管JavaScript语言的重要性在不断的提升，可是它的语言内核基本上没有发生太大变化。
由于JavaScript这门语言在设计上有些地方存在缺陷，或者我们对一些技术点的理解不透彻，会导致编程过程中出现一些脚本语言层面的bug，这类bug往往非常隐蔽，要排查这类bug所花的时间往往也非常多。
这里，我列出是自己实际工作中发现的一些JavaScript陷阱，这个列表今后可能会持续增加。

parseInt
parseInt是将字符串转换为整数的函数，这个函数有个基模式参数，该参数默认是10，即以将字符串转换为十进制的整数。当字符串以0开头时，JavaScript会八进制方式求值。
PLAIN TEXT
JAVASCRIPT:




var s = '08';


parseInt&#40;s&#41;; //0


parseInt&#40;s, 10&#41;; //8 






上面的代码中，第二行是忽略了基模式参数，当遇上字符串08时，JavaScript会用八进制方式来求值，而8在八进制中不是一个有效数字，所以求值结果是0。第三行是为parseInt指定了基模式是10，所以按着十进制方式求值后，结果为8，该结果正确。所以在使用parseInt时带上基模式参数是一个好习惯。

typeof
typeof是返回一个用来表示表达式的数据类型的字符串。下面几种情况返回的类型值是错误的：
PLAIN TEXT
JAVASCRIPT:




typeof null; //object, not null 






上面的代码中，检查null的类型返回object，而并非null。
PLAIN TEXT
JAVASCRIPT:




var a = new String&#40;'Hello'&#41;;


typeof a; //object, not string 






上面的代码中，检查字符串Hello的类型返回object，而并非string。对于Boolean、Number等简单数据类型也存在同样的错误。解决方法是封装自己的类型检查函数：
PLAIN TEXT
JAVASCRIPT:




function isString&#40;object&#41; &#123;


&#160; &#160; return Object.prototype.toString.call&#40;object&#41; == "[object String]";


&#125;


var a = new String&#40;'Hello'&#41;;


isString&#40;a&#41;; //true 







NaN
表示算术表达式返回非数字值的特殊值。NaN 不与任何值相等，包括其本身。
PLAIN TEXT
JAVASCRIPT:




NaN === NaN; //false


typeof NaN; //number


isNaN&#40;NaN&#41;; //true 






上面的代码中，第一行验证了NaN不与任何值相等。第二行与第三行有点矛盾，第二行显示NaN的类型为number，第三行却实现NaN不是一个数字类型的值，因此还是封装一个数字类型的检查函数保险。
PLAIN TEXT
JAVASCRIPT:




function isNumber&#40;object&#41; &#123;


&#160; &#160; return Object.prototype.toString.call&#40;object&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>从1996年网景公司第一次介绍JavaScript，这门语言自今已经有了10多年的历史了，随着互联网技术的发展，JavaScript也从诞生时只完成简单的表单验证发展成互联网上非常重要的一门语言，用它开发的网络应用已经五花八门，许多产品的功能已经接近了桌面产品。尽管JavaScript语言的重要性在不断的提升，可是它的语言内核基本上没有发生太大变化。</p>
<p>由于JavaScript这门语言在设计上有些地方存在缺陷，或者我们对一些技术点的理解不透彻，会导致编程过程中出现一些脚本语言层面的bug，这类bug往往非常隐蔽，要排查这类bug所花的时间往往也非常多。<span id="more-302"></span></p>
<p>这里，我列出是自己实际工作中发现的一些JavaScript陷阱，这个列表今后可能会持续增加。</p>
<ul>
<li><strong>parseInt</strong>
<p>parseInt是将字符串转换为整数的函数，这个函数有个基模式参数，该参数默认是10，即以将字符串转换为十进制的整数。当字符串以0开头时，JavaScript会八进制方式求值。</p>
<div class="igBar"><span id="ljavascript-8"><a href="#" onclick="javascript:showCodeTxt('javascript-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-8">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> s = <span style="color: #3366CC;">'08'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">parseInt<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">parseInt<span style="color: #66cc66;">&#40;</span>s, <span style="color: #CC0000;color:#800000;">10</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//8 </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上面的代码中，第二行是忽略了基模式参数，当遇上字符串08时，JavaScript会用八进制方式来求值，而8在八进制中不是一个有效数字，所以求值结果是0。第三行是为parseInt指定了基模式是10，所以按着十进制方式求值后，结果为8，该结果正确。所以在使用parseInt时带上基模式参数是一个好习惯。</p>
</li>
<li><strong>typeof</strong>
<p>typeof是返回一个用来表示表达式的数据类型的字符串。下面几种情况返回的类型值是错误的：</p>
<div class="igBar"><span id="ljavascript-9"><a href="#" onclick="javascript:showCodeTxt('javascript-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-9">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #003366; font-weight: bold;">null</span>; <span style="color: #009900; font-style: italic;">//object, not null </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上面的代码中，检查null的类型返回object，而并非null。</p>
<div class="igBar"><span id="ljavascript-10"><a href="#" onclick="javascript:showCodeTxt('javascript-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-10">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">typeof</span> a; <span style="color: #009900; font-style: italic;">//object, not string </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上面的代码中，检查字符串Hello的类型返回object，而并非string。对于Boolean、Number等简单数据类型也存在同样的错误。解决方法是封装自己的类型检查函数：</p>
<div class="igBar"><span id="ljavascript-11"><a href="#" onclick="javascript:showCodeTxt('javascript-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-11">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> isString<span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> Object.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">"[object String]"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Hello'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">isString<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//true </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
<li><strong>NaN</strong>
<p>表示算术表达式返回非数字值的特殊值。NaN 不与任何值相等，包括其本身。</p>
<div class="igBar"><span id="ljavascript-12"><a href="#" onclick="javascript:showCodeTxt('javascript-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-12">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">NaN === NaN; <span style="color: #009900; font-style: italic;">//false</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">typeof</span> NaN; <span style="color: #009900; font-style: italic;">//number</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">isNaN<span style="color: #66cc66;">&#40;</span>NaN<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//true </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上面的代码中，第一行验证了NaN不与任何值相等。第二行与第三行有点矛盾，第二行显示NaN的类型为number，第三行却实现NaN不是一个数字类型的值，因此还是封装一个数字类型的检查函数保险。</p>
<div class="igBar"><span id="ljavascript-13"><a href="#" onclick="javascript:showCodeTxt('javascript-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-13">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> isNumber<span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> Object.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>object<span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">"[object Number]"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
<li><strong>隐形Boolean转换</strong>
<p>在流程控制的条件中，如果条件变量不是布尔类型值，JavaScript会对条件变量做隐性：'',NaN,null,undefined,0这几个转换为false，其他值都转换为true。特别需要注意的是，只有空字符串转换为false，其他非空字符串都会转换为true，这里尤其要小心的是'0'，它也被认为是true（因为它非空嘛）。</p>
<li><strong>比较大小</strong>
<p>编程时经常会在条件判断中比较两个数值的大小，当被比较的两个变量都是数值类型时，不会产生问题，假如双方为字符串类型时，比较就可能产生问题了。</p>
<div class="igBar"><span id="ljavascript-14"><a href="#" onclick="javascript:showCodeTxt('javascript-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-14">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #3366CC;">'10'</span>&gt;<span style="color: #3366CC;">'2'</span> <span style="color: #009900; font-style: italic;">//false </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>上面的代码返回false，原因比较两个字符串是根据字符本身的ASCII码的大小，从左往右进行比较。'1'的ASCII码是49，'2'的ASCII码是50，所以'10'小于'2'。为了避免字符串比较带来的问题，比较前最好确保所比较的变量都是数值类型（至少保证一方为数值，在比较时会先将另一方隐性转换为数值类型，再做比较）。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/06/30/javascript-trap/feed/</wfw:commentRss>
		</item>
		<item>
		<title>显示JavaScript函数调用堆栈的方法</title>
		<link>http://blog.tugai.net/2010/06/20/ie-console-trace/</link>
		<comments>http://blog.tugai.net/2010/06/20/ie-console-trace/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 07:42:25 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=287</guid>
		<description><![CDATA[许多大型的JavaScript应用程序间的函数调用关系是非常复杂的，在开发或者调试过程中，经常需要跟踪某个函数是由哪些函数调用后才触发执行的，弄清楚这些函数的调用顺序对我们理解代码的数据流向是非常重要的。
Firebug提供了console.trace()来显示函数堆栈，在需要调试的地方加上下面的一行代码就能显示该函数调用时的上下文关系。IE6就没有这么方便了，它没有提供显示函数堆栈的工具，当不可避免的需要在IE6下调试代码时，使用下面的代码能够显示函数堆栈（建议将下面的JavaScript代码保存为console.trace.js，通过外部引入js的方式引用到页面）：
PLAIN TEXT
JAVASCRIPT:




/**


 * 获取函数名称


 *


 * @param {Function} func 函数引用


 * @return {String} 函数名称


 */


function getFunctionName&#40;func&#41; &#123;


&#160; &#160; if &#40; typeof func == 'function' &#124;&#124; typeof func == 'object' &#41; &#123;


&#160; &#160; &#160; &#160; var name = &#40;'' + func&#41;.match&#40;/function\s*&#40;&#91;\w\$&#93;*&#41;\s*\&#40;/&#41;;


&#160; &#160; &#125;


&#160; &#160; return name &#38;&#38; name&#91;1&#93;;


&#125;


&#160;


if &#40;!&#40;'console' in window&#41;&#41; &#123;


&#160; &#160; window.console = &#123;&#125;;


&#125;


if [...]]]></description>
			<content:encoded><![CDATA[<p>许多大型的JavaScript应用程序间的函数调用关系是非常复杂的，在开发或者调试过程中，经常需要跟踪某个函数是由哪些函数调用后才触发执行的，弄清楚这些函数的调用顺序对我们理解代码的数据流向是非常重要的。</p>
<p><a href="http://getfirebug.com/" target="_blank">Firebug</a>提供了<a href="http://getfirebug.com/wiki/index.php/Console_API#console.trace.28.29" target="_blank">console.trace()</a>来显示函数堆栈，在需要调试的地方加上下面的一行代码就能显示该函数调用时的上下文关系。IE6就没有这么方便了，它没有提供显示函数堆栈的工具，<span id="more-287"></span>当不可避免的需要在IE6下调试代码时，使用下面的代码能够显示函数堆栈（建议将下面的JavaScript代码保存为console.trace.js，通过外部引入js的方式引用到页面）：</p>
<div class="igBar"><span id="ljavascript-16"><a href="#" onclick="javascript:showCodeTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-16">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">/**</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;"> * 获取函数名称</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;"> *</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;"> * @param {Function} func 函数引用</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;"> * @return {String} 函数名称</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;"> */</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> getFunctionName<span style="color: #66cc66;">&#40;</span>func<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> func == <span style="color: #3366CC;">'function'</span> || <span style="color: #000066; font-weight: bold;">typeof</span> func == <span style="color: #3366CC;">'object'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">''</span> + func<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">match</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/<span style="color: #003366; font-weight: bold;">function</span>\s*<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>\w\$<span style="color: #66cc66;">&#93;</span>*<span style="color: #66cc66;">&#41;</span>\s*\<span style="color: #66cc66;">&#40;</span>/</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">name</span> &amp;&amp; <span style="color: #000066;">name</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #66cc66;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'console'</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; window.<span style="color: #006600;">console</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!console.<span style="color: #006600;">trace</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">/**</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;* 显示函数堆栈&lt;br/&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;* 为了和Firebug统一，将trace方法添加到console对象中</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;*</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;* @param {Function} func 函数引用</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;*</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;* @example</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">function a() {</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">b();</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">}</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">function b() {</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">c();</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">function c() {</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">d();</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">}</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">function d() {</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">console.trace();</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">}</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">a();</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">&nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; console.<span style="color: #006600;">trace</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> stack = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caller = arguments.<span style="color: #006600;">callee</span>.<span style="color: #006600;">caller</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>caller<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.<span style="color: #006600;">unshift</span><span style="color: #66cc66;">&#40;</span>getFunctionName<span style="color: #66cc66;">&#40;</span>caller<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caller = caller &amp;&amp; caller.<span style="color: #006600;">caller</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'functions on stack:'</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> + stack.<span style="color: #006600;">join</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/06/20/ie-console-trace/feed/</wfw:commentRss>
		</item>
		<item>
		<title>编写高压缩比的JavaScript代码</title>
		<link>http://blog.tugai.net/2010/06/12/high-compression-ratio-javascript/</link>
		<comments>http://blog.tugai.net/2010/06/12/high-compression-ratio-javascript/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 06:59:57 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=273</guid>
		<description><![CDATA[网站发布前的JavaScript代码压缩是大部分Web前端工程师必做的一项工作，常用的JavaScript压缩工具有Dean Edwards写的JavaScript Compressor、YUI Compressor和Closure Compiler。本文中列出的编码原则是笔者在编程过程中总结出来的十条原则，这些原则适用于一般的JavaScript压缩工具，不适用于Closure Compiler的高级模式（由于Closure Compiler的高级模式过于智能，会用最优的方式重新组织代码结构）。

一次声明多个变量。
在代码块顶部统一声明所有的变量。使用一次var声明多个变量，减少代码使用var的次数。（Closure Complier会自动合并变量声明）
不好的编码：
PLAIN TEXT
JAVASCRIPT:




&#40;function&#40;&#41; &#123;


var name;


var id = 100;


var title;


&#125;&#41;&#40;&#41;; 






推荐的编码：
PLAIN TEXT
JAVASCRIPT:




//节约了10个字节


&#40;function&#40;&#41; &#123;


var name, title, id = 100;


&#125;&#41;&#40;&#41;; 








保存多次使用的字符串。
代码中有些字符串会反复多次使用，如css选择器、事件名称、常用css类，将这些字符串保存在变量中，减少在代码中使用明文字符串的次数。（Closure Complier压缩时会用明文字符串替换变量名，这反而使代码字节数增加了。）
不好的编码：
PLAIN TEXT
JAVASCRIPT:




&#40;function&#40;&#41; &#123;


&#160; &#160; //CSS Selector


&#160; &#160; $&#40;'div.test span'&#41;.text&#40;'OK'&#41;;


&#160; &#160; $&#40;'div.test span'&#41;.css&#40;'width', 100&#41;;


&#160;


&#160; &#160; //Event names


&#160; &#160; $&#40;'#a'&#41;.bind&#40;'click', function&#40;&#41;&#123;&#125;&#41;;


&#160; &#160; $&#40;document&#41;.bind&#40;'click', function&#40;&#41;&#123;&#125;&#41;;


&#160;


&#160; &#160; //CSS Class name


&#160; &#160; $&#40;'#d1'&#41;.addClass&#40;'hidden'&#41;;


&#160; &#160; $&#40;'#d2'&#41;.addClass&#40;'hidden'&#41;;


&#125;&#41;&#40;&#41;; 






推荐的编码：
PLAIN [...]]]></description>
			<content:encoded><![CDATA[<p>网站发布前的JavaScript代码压缩是大部分Web前端工程师必做的一项工作，常用的JavaScript压缩工具有<a href="http://dean.edwards.name/" target="_blank">Dean Edwards</a>写的<a href="http://dean.edwards.name/packer/" target="_blank">JavaScript Compressor</a>、<a href="http://developer.yahoo.com/yui/compressor/" target="_blank">YUI Compressor</a>和<a href="http://code.google.com/closure/compiler/" target="_blank">Closure Compiler</a>。本文中列出的编码原则是笔者在编程过程中总结出来的十条原则，这些原则适用于一般的JavaScript压缩工具，不适用于Closure Compiler的高级模式（由于Closure Compiler的高级模式过于智能，会用最优的方式重新组织代码结构）。<span id="more-273"></span></p>
<ol>
<li><strong>一次声明多个变量。</strong><br />
在代码块顶部统一声明所有的变量。使用一次var声明多个变量，减少代码使用var的次数。（Closure Complier会自动合并变量声明）<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-37"><a href="#" onclick="javascript:showCodeTxt('javascript-37'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-37">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> id = <span style="color: #CC0000;color:#800000;">100</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> title;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-38"><a href="#" onclick="javascript:showCodeTxt('javascript-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-38">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//节约了10个字节</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span>, title, id = <span style="color: #CC0000;color:#800000;">100</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>保存多次使用的字符串。</strong><br />
代码中有些字符串会反复多次使用，如css选择器、事件名称、常用css类，将这些字符串保存在变量中，减少在代码中使用明文字符串的次数。（Closure Complier压缩时会用明文字符串替换变量名，这反而使代码字节数增加了。）<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-39"><a href="#" onclick="javascript:showCodeTxt('javascript-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-39">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//CSS Selector</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div.test span'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'OK'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div.test span'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'width'</span>, <span style="color: #CC0000;color:#800000;">100</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//Event names</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#a'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//CSS Class name</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#d1'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'hidden'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#d2'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'hidden'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-40"><a href="#" onclick="javascript:showCodeTxt('javascript-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-40">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//CSS Selector</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> cssSelector = <span style="color: #3366CC;">'div.test span'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>cssSelector<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'OK'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>cssSelector<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'width'</span>, <span style="color: #CC0000;color:#800000;">100</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//Event names</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> click = <span style="color: #3366CC;">'click'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#a'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span>click, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span>click, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//CSS Class name</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> hidden = <span style="color: #3366CC;">'hidden'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#d1'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>hidden<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#d2'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>hidden<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
<p>用YUI Compressor压缩后的代码：</p>
<div>
<div class="igBar"><span id="ljavascript-41"><a href="#" onclick="javascript:showCodeTxt('javascript-41'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-41">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a = <span style="color: #3366CC;">"div.test span"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"OK"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"width"</span>, <span style="color: #CC0000;color:#800000;">100</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> b = <span style="color: #3366CC;">"click"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"#a"</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span>b, <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span>b, <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> c = <span style="color: #3366CC;">"hidden"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"#d1"</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"#d2"</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>缓存命名空间。</strong><br />
在使用引入了命名空间的Js框架（像YUI、dojo）编程时，命名空间一些的负面作用：一是增加了拼写的复杂度，二是增加了代码的字节长度。缓存层级较深命名空间有利于提高编程效率和减少代码长度。另外该原则也适用于对象的属性、方法，如document.title、window.location.href、Array.prototype.slice、navigator等。<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-42"><a href="#" onclick="javascript:showCodeTxt('javascript-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-42">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> oElement1 = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"element1"</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; oElement2 = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"element2"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> fnCallback<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"click"</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span>oElement1, <span style="color: #3366CC;">"click"</span>, fnCallback<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span>oElement2, <span style="color: #3366CC;">"click"</span>, fnCallback<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-43"><a href="#" onclick="javascript:showCodeTxt('javascript-43'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-43">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> oElement1 = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"element1"</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; oElement2 = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"element2"</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; addListener = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> fnCallback<span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"click"</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">addListener<span style="color: #66cc66;">&#40;</span>oElement1, <span style="color: #3366CC;">"click"</span>, fnCallback<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">addListener<span style="color: #66cc66;">&#40;</span>oElement2, <span style="color: #3366CC;">"click"</span>, fnCallback<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>缓存关键字。</strong><br />
压缩工具对JavaScript关键字都不会做处理，对一些常用的关键字（如undefined、true、false、window, document）缓存，有利于提高代码压缩率。<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-44"><a href="#" onclick="javascript:showCodeTxt('javascript-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-44">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a, b = <span style="color: #003366; font-weight: bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> a === <span style="color: #3366CC;">'undefined'</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//do something.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-45"><a href="#" onclick="javascript:showCodeTxt('javascript-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-45">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//const</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> UNDEFINED, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">TRUE</span> = <span style="color: #CC0000;color:#800000;">1</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; a, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b = <span style="color: #003366; font-weight: bold;">TRUE</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>a === UNDEFINED<span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//do something.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>用&#038;&替换if条件判断。</strong><br />
该原则牺牲了代码可读性，Closure Complier可以自动替换，酌情使用。<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-46"><a href="#" onclick="javascript:showCodeTxt('javascript-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-46">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-47"><a href="#" onclick="javascript:showCodeTxt('javascript-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-47">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a &amp;&amp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>用三元运算符替换if else。</strong><br />
该原则牺牲了代码可读性，Closure Complier可以自动替换，酌情使用。<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showCodeTxt('javascript-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-48">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">throw</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'error'</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-49"><a href="#" onclick="javascript:showCodeTxt('javascript-49'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-49">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a ? <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> : <span style="color: #000066; font-weight: bold;">throw</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'error'</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>避免使用全局变量。</strong><br />
尽量避免使用全局变量，代码都应该封装在闭包内。除了Closure Complier的高级模式外，其他的压缩工具都不会修改全局变量。<br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-50"><a href="#" onclick="javascript:showCodeTxt('javascript-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-50">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> title = <span style="color: #3366CC;">'test'</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-51"><a href="#" onclick="javascript:showCodeTxt('javascript-51'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-51">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> title = <span style="color: #3366CC;">'test'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>用||为变量赋默认值。</strong><br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-52"><a href="#" onclick="javascript:showCodeTxt('javascript-52'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-52">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> setName<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #000066;">name</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Joe'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-53"><a href="#" onclick="javascript:showCodeTxt('javascript-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-53">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> setName<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> = <span style="color: #000066;">name</span> || <span style="color: #3366CC;">'Joe'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>使用数组和对象的字面量。</strong><br />
不好的编码：</p>
<div class="igBar"><span id="ljavascript-54"><a href="#" onclick="javascript:showCodeTxt('javascript-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-54">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">b = <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<div>推荐的编码：</p>
<div class="igBar"><span id="ljavascript-55"><a href="#" onclick="javascript:showCodeTxt('javascript-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-55">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">b = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</div>
</li>
<li><strong>编写自己的通用函数。</strong><br />
常用的函数有：</p>
<div class="igBar"><span id="ljavascript-56"><a href="#" onclick="javascript:showCodeTxt('javascript-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-56">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> returnFalse<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> returnTrue<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> emptyFunction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/06/12/high-compression-ratio-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>第八期Web标准化交流会广州分会</title>
		<link>http://blog.tugai.net/2010/06/03/w3c-no8-guangzhou/</link>
		<comments>http://blog.tugai.net/2010/06/03/w3c-no8-guangzhou/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 02:41:17 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Web标准化交流会]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=261</guid>
		<description><![CDATA[上周末参加了广州的第八期Web标准化交流会，会议是在珠江新城的一家印度餐厅举行的。我非常荣幸的作为嘉宾和大家做了一个小时左右的分享，我分享的主题是“网站性能优化”，网站性能优化是一个非常大的范畴，开始做PPT的时候，我写的JavaScript性能优化，但在制作的过程中发现有些内容不属于JavaScript优化的范畴，而属于网站性能优化的范畴，所以就用了这个名字。PPT中是拿出来平时工作用到的几种优化方法做分享。本来林毅要我介绍一下YSlow和Page Speed的用法，可我觉得这两个工具太常用了，也没有什么使用技巧好交流的。也许参加交流会的很多朋友都不是做JavaScript开发的，所以在整个分享过程中没有什么提问，这也是我在选择分享主题时没有考虑的一个地方，下次一定要根据会场听众的知识结构准备演讲主题！
这是交流会用的PPT，感兴趣的同学点击下载PPT文档.



]]></description>
			<content:encoded><![CDATA[<p>上周末参加了广州的第八期<a href="http://www.w3ctech.com/" target="_blank">Web标准化交流会</a>，会议是在珠江新城的一家印度餐厅举行的。我非常荣幸的作为嘉宾和大家做了一个小时左右的分享，我分享的主题是“网站性能优化”，网站性能优化是一个非常大的范畴，开始做PPT的时候，我写的JavaScript性能优化，但在制作的过程中发现有些内容不属于JavaScript优化的范畴，而属于网站性能优化的范畴，所以就用了这个名字。PPT中是拿出来平时工作用到的几种优化方法做分享。本来<a href="http://www.gzlinyi.cn">林毅</a>要我介绍一下<a href="developer.yahoo.com/yslow/">YSlow</a>和<a href="code.google.com/speed/page-speed/">Page Speed</a>的用法，可我觉得这两个工具太常用了，也没有什么使用技巧好交流的<span id="more-261"></span>。也许参加交流会的很多朋友都不是做JavaScript开发的，所以在整个分享过程中没有什么提问，这也是我在选择分享主题时没有考虑的一个地方，下次一定要根据会场听众的知识结构准备演讲主题！</p>
<p>这是交流会用的PPT，感兴趣的同学<a href="/wp-content/uploads/2010/06/w3c_no8.rar">点击下载PPT文档</a>.</p>
<p><a href="/wp-content/uploads/2010/06/webe6a087e58786e58c96e4baa4e6b581e4bc9a-13.jpg"><img src="/wp-content/uploads/2010/06/webe6a087e58786e58c96e4baa4e6b581e4bc9a-13-300x192.jpg" alt="webe6a087e58786e58c96e4baa4e6b581e4bc9a-13" title="webe6a087e58786e58c96e4baa4e6b581e4bc9a-13" width="300" height="192" class="alignleft size-medium wp-image-267" /></a></p>
<p><a href="/wp-content/uploads/2010/06/img_4340thumb.jpg"><img src="/wp-content/uploads/2010/06/img_4340thumb-300x225.jpg" alt="img_4340thumb" title="img_4340thumb" width="300" height="225" class="alignnone size-medium wp-image-265" /></a></p>
<p><a href="/wp-content/uploads/2010/06/img_4338thumb.jpg"><img src="/wp-content/uploads/2010/06/img_4338thumb-300x238.jpg" alt="img_4338thumb" title="img_4338thumb" width="300" height="238" class="alignleft size-medium wp-image-264" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/06/03/w3c-no8-guangzhou/feed/</wfw:commentRss>
		</item>
		<item>
		<title>在右键中添加“用vim编辑”项</title>
		<link>http://blog.tugai.net/2010/05/26/edit-vi-contextmenu/</link>
		<comments>http://blog.tugai.net/2010/05/26/edit-vi-contextmenu/#comments</comments>
		<pubDate>Wed, 26 May 2010 07:20:48 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[其它]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/2010/05/26/%e5%9c%a8%e5%8f%b3%e9%94%ae%e4%b8%ad%e6%b7%bb%e5%8a%a0%e2%80%9c%e7%94%a8vim%e7%bc%96%e8%be%91%e2%80%9d%e9%a1%b9/</guid>
		<description><![CDATA[[方法一]
首先介绍比较简单的方法(VIM Tip #1440)：
1. 打开“开始菜单à运行”，输入“sendto”，然后选择确定，这会打开一个快捷方式文件夹；
2. 右键在此文件夹中的空白位置单击，选择“新建à快捷方式”，增加一个快捷方式；
3. 在“项目位置”一栏中输入下面的内容：
"C:\Program Files\Vim\vim70\gvim.exe" -p --remote-tab-silent "%*"
4. 输入快捷方式的名字为“gVim”；
现在，当你在文件浏览器中右键单击想打开的文件后，选择“发送到àgVim”，就会在gVim中新开一个标签页，打开这个文件，是不是很方便？
[方法二]
下面介绍方法二(VIM Tip #1314)，和方法一相比稍麻烦一点。
如果你已经安装了VIM 7.0，并在安装过程中选择了“安装右键菜单”，那么你需要在注册表删除“HKCR\*\shellex\ContextMenuHandlers\gvim\”主键，听起来很麻烦？
没关系，我们有简单的方法，不需要手动到注册表里删除，只需要卸载VIM 7.0，在卸载的过程中选择删除右键菜单，这样右键菜单就被删除。然后再重新安装VIM 7.0，在安装的过程中，不要再安装右键菜单。
完成这一步之后，把下面的内容保存到一个文件中，取名为vim.reg：
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\Shell\Edit with Vim]
@="Edit with &#038;Vim"
[HKEY_CLASSES_ROOT\*\Shell\Edit with Vim\command]
@="\"C:\\Program Files\\Vim\\vim70\\gvim.exe\" -p --remote-tab-silent \"%1\""
接下来用鼠标双击vim.reg文件，把上述内容导入到注册表中。
OK，现在你用鼠标右标想打开的文件，看到“Edit with Vim”项了吗？选中此项，就会在一个新的标签页中打开该文件了。
这种方法虽然比方法一麻烦了一点，不过“Edit with Vim”在右键菜单里排在“发送到”之前，对一个每天数十次上百次使用的功能来说，这一点“偷懒”能省不少事呢！
]]></description>
			<content:encoded><![CDATA[<p>[方法一]</p>
<p>首先介绍比较简单的方法(VIM Tip #1440)：</p>
<p>1. 打开“开始菜单à运行”，输入“sendto”，然后选择确定，这会打开一个快捷方式文件夹；</p>
<p>2. 右键在此文件夹中的空白位置单击，选择“新建à快捷方式”，增加一个快捷方式；</p>
<p>3. 在“项目位置”一栏中输入下面的内容：</p>
<p>"C:\Program Files\Vim\vim70\gvim.exe" -p --remote-tab-silent "%*"</p>
<p>4. 输入快捷方式的名字为“gVim”；<span id="more-258"></span></p>
<p>现在，当你在文件浏览器中右键单击想打开的文件后，选择“发送到àgVim”，就会在gVim中新开一个标签页，打开这个文件，是不是很方便？</p>
<p>[方法二]</p>
<p>下面介绍方法二(VIM Tip #1314)，和方法一相比稍麻烦一点。</p>
<p>如果你已经安装了VIM 7.0，并在安装过程中选择了“安装右键菜单”，那么你需要在注册表删除“HKCR\*\shellex\ContextMenuHandlers\gvim\”主键，听起来很麻烦？</p>
<p>没关系，我们有简单的方法，不需要手动到注册表里删除，只需要卸载VIM 7.0，在卸载的过程中选择删除右键菜单，这样右键菜单就被删除。然后再重新安装VIM 7.0，在安装的过程中，不要再安装右键菜单。</p>
<p>完成这一步之后，把下面的内容保存到一个文件中，取名为vim.reg：</p>
<p>Windows Registry Editor Version 5.00</p>
<p>[HKEY_CLASSES_ROOT\*\Shell\Edit with Vim]<br />
@="Edit with &#038;Vim"<br />
[HKEY_CLASSES_ROOT\*\Shell\Edit with Vim\command]<br />
@="\"C:\\Program Files\\Vim\\vim70\\gvim.exe\" -p --remote-tab-silent \"%1\""</p>
<p>接下来用鼠标双击vim.reg文件，把上述内容导入到注册表中。</p>
<p>OK，现在你用鼠标右标想打开的文件，看到“Edit with Vim”项了吗？选中此项，就会在一个新的标签页中打开该文件了。</p>
<p>这种方法虽然比方法一麻烦了一点，不过“Edit with Vim”在右键菜单里排在“发送到”之前，对一个每天数十次上百次使用的功能来说，这一点“偷懒”能省不少事呢！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/05/26/edit-vi-contextmenu/feed/</wfw:commentRss>
		</item>
		<item>
		<title>理解Closure Compiler强加的约束</title>
		<link>http://blog.tugai.net/2010/04/05/closure-compiler-restrictions/</link>
		<comments>http://blog.tugai.net/2010/04/05/closure-compiler-restrictions/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 12:10:11 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[Closure Compiler]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=254</guid>
		<description><![CDATA[Closure Compiler期望输入的JavaScript符合一定的约束条件。压缩级别越高，约束条件越多。
本文分别介绍每种级别的条件约束。
适用于所有级别的约束
下面两条约束条件适用于Closure Compiler（以下简称“编译器”）的所有级别压缩：

编译器只能识别Ecmascript 262第三版
Ecmascript第三版是Javascript 1.5 和 JScript 5.5的基础，当人们提到“JavaScript”时，通常指的是这个版本的JavaScript。编译器不支持任何版本的JScript或者高于1.5版本的JavaScript。
符合Ecmascript语言规范的浏览器特有特性能够很好的在编译器中工作，例如用合法JavaScript语法创建的ActiveX对象；不符合Ecmascript语言规范的浏览器特有特性会导致编译器错误，例如，Firefox的JavaScript引擎支持const关键字，但JavaScript规范中没有包含这个关键字，所以编译器不支持它。本约束能让编译器检查代码是否兼容所有浏览器。

编译器不保留注释
选择编译器的任何级别压缩都会删除代码中的注释，所以那些依赖特定注释的代码不能用编译器来压缩。
举个例子，因为编译器不会保留代码注释，所以代码中不能直接使用JScript的条件化注释，然而，你可以使用eval()将代码中的条件化注释包起来，编译器可以正确的压缩下面的代码：
PLAIN TEXT
JAVASCRIPT:




x = eval&#40;"/*@cc_on 2+@*/ 0"&#41;; 






提示：你可以使用@preserve annotation参数在压缩后的代码顶部包含如开源代码授权或者其它重要的文字。



SIMPLE_OPTIMIZATIONS级别的约束
SIMPLE_OPTIMIZATIONS级别压缩会重命名函数参数、局部变量、使用内联函数等方法来减少代码体积。然而，有一些JavaScript结构会中断重命名过程。
使用SIMPLE_OPTIMIZATIONS时请避免使用下面的代码结构：

with
当使用with时，编译器不能区分本地变量和一个同名的对象属性，因此它的名称重命名的所有实例。而且，with语句让代码变得难以阅读。

eval()
编译器不会解析eval()的字符串参数，所以也不会对字符串参数中包含的变量重命名。

字符串表示方式的函数或属性名
编译器会重命名函数名和函数参数名，但不会改变代码中通过字符串引用的函数或者参数，应该避免使用字符串方式表示函数或者参数名。


ADVANCED_OPTIMIZATIONS级别的约束
ADVANCED_OPTIMIZATIONS级别压缩执行和SIMPLE_OPTIMIZATIONS一样的代码转换，并且还添加了对全局属性、变量、函数名的重命名，还会删除无用的代码，因此它有着更为严格的约束。
重命名全局变量、函数和属性的启示
ADVANCED_OPTIMIZATIONS的全局重命名使得下面的做法变得十分危险：

未声明的外部引用
为了正确的重命名全局变量、函数和属性，编译器必须清楚的知道哪些地方引用了这些全局对象。必须告诉编译器哪些符号是定义在压缩代码之外的函数。更多相关内容请看破坏已压缩代码和未压缩代码之间的引用。

在外部代码中使用未导出的内部名字
压缩代码必须导出那些需要在未压缩代码中引用的符号。更多相关内容请看破坏已压缩代码和未压缩代码之间的引用

使用字符串方式引用对象的属性
编译器会在高级压缩模式下重命名属性名，但它不会重命名字符串。
PLAIN TEXT
JAVASCRIPT:




var x = &#123; renamed_property: 1 &#125;;


var y = x.renamed_property; // This is OK.


&#160;


// 'renamed_property' below doesn't exist on x after renaming, so the


//&#160; following evaluates to false.


if &#40; 'renamed_property' in x &#41; &#123;&#125;; // BAD


&#160;


// The following also fails:


x&#91;'renamed_property'&#93;; [...]]]></description>
			<content:encoded><![CDATA[<p>Closure Compiler期望输入的JavaScript符合一定的约束条件。压缩级别越高，约束条件越多。</p>
<p>本文分别介绍每种级别的条件约束。</p>
<h4>适用于所有级别的约束</h4>
<p>下面两条约束条件适用于Closure Compiler（以下简称“编译器”）的所有级别压缩：</p>
<ul>
<li>编译器只能识别Ecmascript 262第三版<br />
Ecmascript第三版是Javascript 1.5 和 JScript 5.5的基础，当人们提到“JavaScript”时，通常指的是这个版本的JavaScript。编译器不支持任何版本的JScript或者高于1.5版本的JavaScript。</p>
<p>符合Ecmascript语言规范的浏览器特有特性能够很好的在编译器中工作，例如用合法JavaScript语法创建的ActiveX对象；不符合Ecmascript语言规范的浏览器特有特性会导致编译器错误，例如，Firefox的JavaScript引擎支持const关键字，但JavaScript规范中没有包含这个关键字，所以编译器不支持它。本约束能让编译器检查代码是否兼容所有浏览器。
</li>
<li>编译器不保留注释<br />
选择编译器的任何级别压缩都会删除代码中的注释，所以那些依赖特定注释的代码不能用编译器来压缩。</p>
<p>举个例子，因为编译器不会保留代码注释，所以代码中不能直接使用JScript的条件化注释，然而，你可以使用eval()将代码中的条件化注释包起来，编译器可以正确的压缩下面的代码：</p>
<div class="igBar"><span id="ljavascript-70"><a href="#" onclick="javascript:showCodeTxt('javascript-70'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-70">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">x = <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"/*@cc_on 2+@*/ 0"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>提示：你可以使用<a href="http://code.google.com/closure/compiler/docs/js-for-compiler.html#tag-license" target="_blank">@preserve annotation</a>参数在压缩后的代码顶部包含如开源代码授权或者其它重要的文字。
</li>
</ul>
<p><span id="more-254"></span></p>
<h4>SIMPLE_OPTIMIZATIONS级别的约束</h4>
<p>SIMPLE_OPTIMIZATIONS级别压缩会重命名函数参数、局部变量、使用内联函数等方法来减少代码体积。然而，有一些JavaScript结构会中断重命名过程。</p>
<p>使用SIMPLE_OPTIMIZATIONS时请避免使用下面的代码结构：</p>
<ul>
<li>with<br />
当使用with时，编译器不能区分本地变量和一个同名的对象属性，因此它的名称重命名的所有实例。而且，with语句让代码变得难以阅读。
</li>
<li>eval()<br />
编译器不会解析eval()的字符串参数，所以也不会对字符串参数中包含的变量重命名。
</li>
<li>字符串表示方式的函数或属性名<br />
编译器会重命名函数名和函数参数名，但不会改变代码中通过字符串引用的函数或者参数，应该避免使用字符串方式表示函数或者参数名。
</li>
</ul>
<h4>ADVANCED_OPTIMIZATIONS级别的约束</h4>
<p>ADVANCED_OPTIMIZATIONS级别压缩执行和SIMPLE_OPTIMIZATIONS一样的代码转换，并且还添加了对全局属性、变量、函数名的重命名，还会删除无用的代码，因此它有着更为严格的约束。</p>
<h5>重命名全局变量、函数和属性的启示</h5>
<p>ADVANCED_OPTIMIZATIONS的全局重命名使得下面的做法变得十分危险：</p>
<ul>
<li>未声明的外部引用<br />
为了正确的重命名全局变量、函数和属性，编译器必须清楚的知道哪些地方引用了这些全局对象。必须告诉编译器哪些符号是定义在压缩代码之外的函数。更多相关内容请看<a href="/2010/04/02/closure-compiler-advanced-optimizations#mixed" target="_blank">破坏已压缩代码和未压缩代码之间的引用</a>。
</li>
<li>在外部代码中使用未导出的内部名字<br />
压缩代码必须导出那些需要在未压缩代码中引用的符号。更多相关内容请看<a href="/2010/04/02/closure-compiler-advanced-optimizations#mixed" target="_blank">破坏已压缩代码和未压缩代码之间的引用</a>
</li>
<li>使用字符串方式引用对象的属性<br />
编译器会在高级压缩模式下重命名属性名，但它不会重命名字符串。</p>
<div class="igBar"><span id="ljavascript-71"><a href="#" onclick="javascript:showCodeTxt('javascript-71'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-71">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #66cc66;">&#123;</span> renamed_property: <span style="color: #CC0000;color:#800000;">1</span> <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> y = x.<span style="color: #006600;">renamed_property</span>; <span style="color: #009900; font-style: italic;">// This is OK.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// 'renamed_property' below doesn't exist on x after renaming, so the</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//&nbsp; following evaluates to false.</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'renamed_property'</span> <span style="color: #000066; font-weight: bold;">in</span> x <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>; <span style="color: #009900; font-style: italic;">// BAD</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// The following also fails:</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">x<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'renamed_property'</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #009900; font-style: italic;">// BAD </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
如果你想使用引号+字符串的方式来引用一个属性，那么就请一直保持使用这种方式：</p>
<div class="igBar"><span id="ljavascript-72"><a href="#" onclick="javascript:showCodeTxt('javascript-72'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-72">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #66cc66;">&#123;</span> <span style="color: #3366CC;">'unrenamed_property'</span>: <span style="color: #CC0000;color:#800000;">1</span> <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">x<span style="color: #66cc66;">&#91;</span><span style="color: #3366CC;">'unrenamed_property'</span><span style="color: #66cc66;">&#93;</span>;&nbsp; <span style="color: #009900; font-style: italic;">// This is OK.</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #3366CC;">'unrenamed_property'</span> <span style="color: #000066; font-weight: bold;">in</span> x <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;&nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// This is OK </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
</ul>
<h5>使用变量当作全局变量的属性</h5>
<p>编译器独立的重命名属性和变量。例如，编译器认为以下两个foo是不同引用，即使它们是等价的。</p>
<div class="igBar"><span id="ljavascript-73"><a href="#" onclick="javascript:showCodeTxt('javascript-73'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-73">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> foo = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #006600;">foo</span>; <span style="color: #009900; font-style: italic;">// BAD </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>代码被压缩成</p>
<div class="igBar"><span id="ljavascript-74"><a href="#" onclick="javascript:showCodeTxt('javascript-74'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-74">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> a = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #006600;">b</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>如果想让变量当作全局变量的属性，请使用下面的方法：</p>
<div class="igBar"><span id="ljavascript-75"><a href="#" onclick="javascript:showCodeTxt('javascript-75'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-75">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #006600;">foo</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #006600;">foo</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
<h5>消除无用代码所产生的影响</h5>
<p>高级压缩模式会删除从未使用过的代码，这就使得下面的做法非常危险：</p>
<ul>
<li>从压缩代码外部调用函数<br />
    当压缩那些仅在外部调用的函数时，编译器认为函数从来没有被调用过，就把函数删除了。为了避免不必要的删除，可以使用下面任何一种方法：</p>
<ul>
<li>将程序中的所有代码一起压缩。</li>
<li>导出压缩函数。</li>
</ul>
<p>更多相关内容请看<a href="/2010/04/02/closure-compiler-advanced-optimizations#remove" target="_blank">删除原本想保留的代码</a></p>
</li>
<li>通过迭代构造器或者原型的方式获取函数<br />
为了检测一个函数是否是无用函数，编译器必须查找所有使用该函数的调用。通过循环一个构造器或者原型能够找到并调用这些方法，但编译器不能通过这种方式来识别一个函数。</p>
<p>例如，下面的代码会导致本来需要保留的函数被删除。</p>
<div class="igBar"><span id="ljavascript-76"><a href="#" onclick="javascript:showCodeTxt('javascript-76'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-76">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> Coordinate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Coordinate.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">initX</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">x</span> = <span style="color: #CC0000;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Coordinate.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">initY</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">y</span> = <span style="color: #CC0000;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> coord = <span style="color: #003366; font-weight: bold;">new</span> Coordinate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>method <span style="color: #000066; font-weight: bold;">in</span> Coordinate.<span style="color: #006600;">prototype</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; Coordinate.<span style="color: #006600;">prototype</span><span style="color: #66cc66;">&#91;</span>method<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>coord<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// BAD</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
编译器不能理解initX()和initY()在循环中调用了，所以删除了这两个方法。</p>
<p>如果把一个函数当做参数传递，编译器能够找到该参数的调用。例如，编译器在使用高级模式压缩下面代码时，不会删除getHello()函数。</p>
<div class="igBar"><span id="ljavascript-77"><a href="#" onclick="javascript:showCodeTxt('javascript-77'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-77">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> alertF<span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> getHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'hello'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">// The Compiler figures out that this call to alertF also calls getHello().</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">alertF<span style="color: #66cc66;">&#40;</span>getHello<span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// This is OK. </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
</li>
</ul>
<p>In Advanced mode the Compiler collapses object properties to prepare for name shortening. For example, the Compiler transforms this:</p>
<p>var foo = {};<br />
foo.bar = function (a) { alert(a) };<br />
foo.bar("hello");</p>
<p>into this:</p>
<p>var foo$bar = function (a) { alert(a) };<br />
foo$bar("hello");</p>
<p>This property flattening allows the later renaming pass to rename more efficiently. The Compiler can replace foo$bar with a single character, for example.</p>
<h5>扁平化的对象属性的启示</h5>
<p>为了重命名对象属性名，高级压缩模式会肢解对象属性，例如，编译器将下面的代码</p>
<div class="igBar"><span id="ljavascript-78"><a href="#" onclick="javascript:showCodeTxt('javascript-78'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-78">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> foo = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo.<span style="color: #006600;">bar</span> = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo.<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"hello"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
变成</p>
<div class="igBar"><span id="ljavascript-79"><a href="#" onclick="javascript:showCodeTxt('javascript-79'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-79">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> foo$bar = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo$bar<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"hello"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
扁平化的属性让后来的重命名更有效，编译器用一个字符即可替换foo$bar。
</p>
<p>      To prevent property flattening from breaking your references to this, only use this within constructors and prototype methods. The meaning of this is unambiguous when you call a constructor with the new keyword, or within a function that is a property of a prototype.</p>
<p>扁平化的属性让下面的做法非常危险：</p>
<ul>
<li>在构造器和原型方法外使用this<br />
    扁平化的属性会改变this关键字在函数中的意义，例如：</p>
<div class="igBar"><span id="ljavascript-80"><a href="#" onclick="javascript:showCodeTxt('javascript-80'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-80">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> foo = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo.<span style="color: #006600;">bar</span> = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">bad</span> = a; <span style="color: #66cc66;">&#125;</span>; <span style="color: #009900; font-style: italic;">// BAD</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo.<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"hello"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
压缩后变成：</p>
<div class="igBar"><span id="ljavascript-81"><a href="#" onclick="javascript:showCodeTxt('javascript-81'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-81">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> foo$bar = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">bad</span> = a; <span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo$bar<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"hello"</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
压缩前，foo.bar内的this指向foo，压缩后，this指向全局变量this。像本例这种情况，编译器产生一个告警信息：</p>
<div class="igBar"><span id="lhtml-82"><a href="#" onclick="javascript:showCodeTxt('html-82'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-82">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&quot;WARNING - dangerous use of this in static method foo.bar&quot; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
为了防止扁平化的属性打破this的引用，请仅在构造函数和原型方法中使用this。
    </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/04/05/closure-compiler-restrictions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Closure Compiler三种压缩级别</title>
		<link>http://blog.tugai.net/2010/04/05/closure-compiler-level/</link>
		<comments>http://blog.tugai.net/2010/04/05/closure-compiler-level/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 07:51:46 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[Closure Compiler]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=247</guid>
		<description><![CDATA[Closure Compiler（以下简称“编译器”）提供三种不同级别的压缩模式。
WHITESPACE_ONLY（去空白优化）
该级别会删除代码中的注释、换行符和其他不必要的空格，输出后的代码在功能上和压缩前的代码完全一样。该压缩级别是三种级别中代码压缩最小的。
SIMPLE_OPTIMIZATIONS（简单优化）
该级别包含WHITESPACE_ONLY级别的所有功能，此外，它还提供用短名称重命名局部变量名和函数参数名的功能。用短名称重命名变量可以让代码体积明显的减少。由于该级别压缩只是对函数内部的局部变量重命名，所以不会影响被压缩代码与其他（未用Closure Compiler压缩的）JavaScript代码之间的相互调用。编译器还提供对JavaScript语法验证的功能，只要该代码不使用字符串名称访问本地变量（例如使用eval()）。
该级别是编译器默认的压缩级别。
ADVANCED_OPTIMIZATIONS（高级优化）
该级别包含SIMPLE_OPTIMIZATIONS级别的所有功能，另外，它还添加了许多入侵性代码转换，从而实现三种级别中最高压缩比。使用该级别压缩代码能超越其它JS代码压缩工具（如YUI Compressor），达到最好的代码压缩效果。
为了使用这种极端压缩方式，ADVANCED_OPTIMIZATIONS要求代码在编写时遵循很强的约定。如果代码没有遵从这些约定，那么使用该级别生成的代码将不能运行。
例如，除非对压缩的代码完成某些确保互操作性的指定步骤，否则使用ADVANCED_OPTIMIZATIONS压缩后的代码有可能不能和未压缩的代码一起运行。如果没有对代码中的外部函数引用和外部属性引用做标识，编译器会对这些引用重命名，从而产生压缩代码和外部代码间名称不一致的错误。
更多相关内容请看使用Closure Compiler高级模式需要注意哪些问题？。
ADVANCED_OPTIMIZATIONS会对代码做以下转换：

更多侵入性的重命名
SIMPLE_OPTIMIZATIONS只会对函数参数和函数内部变量重命名，而ADVANCED_OPTIMIZATIONS还会对全局变量、函数名、属性名重命名。

删除无用代码
ADVANCED_OPTIMIZATIONS会删除未使用的代码。当代码和一个很大的JS库一起压缩时，这个功能特别有用。如果代码只用到类库中的小部分函数，编译器输出时会将类库中除这小部分函数外的其它函数都删除。

内联函数（inlining）
ADVANCED_OPTIMIZATIONS会将一些调用函数替换为函数体内容，这种转换被称为“内联函数”，编译器只会对那些它检查后认为安全的函数做内联函数，另外，它还会对它认为安全的常量或变量使用内联方式。


]]></description>
			<content:encoded><![CDATA[<p>Closure Compiler（以下简称“编译器”）提供三种不同级别的压缩模式。</p>
<h4>WHITESPACE_ONLY（去空白优化）</h4>
<p>该级别会删除代码中的注释、换行符和其他不必要的空格，输出后的代码在功能上和压缩前的代码完全一样。该压缩级别是三种级别中代码压缩最小的。</p>
<h4>SIMPLE_OPTIMIZATIONS（简单优化）</h4>
<p>该级别包含WHITESPACE_ONLY级别的所有功能，此外，它还提供用短名称重命名局部变量名和函数参数名的功能。用短名称重命名变量可以让代码体积明显的减少。由于该级别压缩只是对函数内部的局部变量重命名，所以不会影响被压缩代码与其他（未用Closure Compiler压缩的）JavaScript代码之间的相互调用。编译器还提供对JavaScript语法验证的功能，只要该代码不使用字符串名称访问本地变量（例如使用eval()）。</p>
<p>该级别是编译器默认的压缩级别。</p>
<h4>ADVANCED_OPTIMIZATIONS（高级优化）</h4>
<p>该级别包含SIMPLE_OPTIMIZATIONS级别的所有功能，另外，它还添加了许多入侵性代码转换，从而实现三种级别中最高压缩比。使用该级别压缩代码能超越其它JS代码压缩工具（如YUI Compressor），达到最好的代码压缩效果。</p>
<p>为了使用这种极端压缩方式，ADVANCED_OPTIMIZATIONS要求代码在编写时遵循很强的约定。如果代码没有遵从这些约定，那么使用该级别生成的代码将不能运行。</p>
<p>例如，除非对压缩的代码完成某些确保互操作性的指定步骤，否则使用ADVANCED_OPTIMIZATIONS压缩后的代码有可能不能和未压缩的代码一起运行。如果没有对代码中的外部函数引用和外部属性引用做标识，编译器会对这些引用重命名，从而产生压缩代码和外部代码间名称不一致的错误。</p>
<p>更多相关内容请看<a href="/2010/04/02/closure-compiler-advanced-optimizations" target="_blank">使用Closure Compiler高级模式需要注意哪些问题？</a>。</p>
<p>ADVANCED_OPTIMIZATIONS会对代码做以下转换：</p>
<ul>
<li>更多侵入性的重命名<br />
SIMPLE_OPTIMIZATIONS只会对函数参数和函数内部变量重命名，而ADVANCED_OPTIMIZATIONS还会对全局变量、函数名、属性名重命名。
</li>
<li>删除无用代码<br />
ADVANCED_OPTIMIZATIONS会删除未使用的代码。当代码和一个很大的JS库一起压缩时，这个功能特别有用。如果代码只用到类库中的小部分函数，编译器输出时会将类库中除这小部分函数外的其它函数都删除。
</li>
<li>内联函数（inlining）<br />
ADVANCED_OPTIMIZATIONS会将一些调用函数替换为函数体内容，这种转换被称为“内联函数”，编译器只会对那些它检查后认为安全的函数做内联函数，另外，它还会对它认为安全的常量或变量使用内联方式。
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/04/05/closure-compiler-level/feed/</wfw:commentRss>
		</item>
		<item>
		<title>使用Closure Compiler service压缩代码</title>
		<link>http://blog.tugai.net/2010/04/01/closure-compiler-service/</link>
		<comments>http://blog.tugai.net/2010/04/01/closure-compiler-service/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 17:39:33 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[Closure Compiler]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=235</guid>
		<description><![CDATA[使用Closure Compiler service是熟悉Closure Compiler压缩最简单的方法。

在新的窗口或Tab中访问Closure Compiler UI：http://closure-compiler.appspot.com
你能看到预制了一个简单的Hello World函数的Closure Compiler UI。
点击“Compile”查看结果：

仅此而已！现在你有了一个函数名和源代码一样但体积小了许多的JavaScript代码。Closure Compiler service通过删除注释、空格和重命名基符号等方法将代码字节数从92减少到55。
为了让你更方便，Closure Compiler service将代码输出为default.js并在服务器上保存一小时。你能够从上面的输出面板中拷贝default.js链接的地址并直接访问这个文件。如果你在一个小时内修改了原始JavaScript代码并重新编译，只要你不修改文本域顶部的@output_file_name参数，那么Closure Compiler service会用编译后的新代码覆盖之前的输出文件。利用这个特性，你就可以在测试环境下，通过在网页上直接引入JS文件的方法，快速测试代码。（请不要在生产环境中直接引用该文件）
注意：为了防止被滥用，Closure Compiler service会限制连续使用代码编译的次数。当你遇上系统提示“Too many compiles performed recently”时，这就意味着你暂时超过了这个限制的次数，请稍后再试。
优化JavaScript文件
Closure Compiler service可以优化一个或多个JavaScript文件。

将http://code.google.com/closure/compiler/samples/tutorial2.js粘贴到“Add a URL”文本框中。这个文件包含未优化的（在DOM树中创建节点的）代码。
点击“Add”。（如果有多个文件，请重复第一步和第二步，直到你添加完所有的文件）
如果需要指定压缩后的文件名，请修改文本域的顶部的@output_file_name参数。默认的输出文件名是default.js，但你可以根据项目实际情况为它取一个更有意义的名字。再次提醒，服务器对输出文件只保留一个小时。
点击“Compile”。

优化后的JavaScript代码显示在下图右手边的面板中。
为了使用优化后的代码，你可以复制代码并粘贴到你的源文件中，或者下载该文件到你电脑的适合目录中，或者在script标签中直接链接到该文件（一小时内有效）。
这仅仅是完成了使用Closure Compiler service UI对简单函数的优化例子。如果你希望使用Closure Compiler优化一个大系统代码，你需要直接和Closure Compiler service API对话。相关的更多内容请看《Closure Compiler Service API》
原文地址：http://code.google.com/closure/compiler/docs/gettingstarted_ui.html
]]></description>
			<content:encoded><![CDATA[<p>使用Closure Compiler service是熟悉Closure Compiler压缩最简单的方法。</p>
<ul>
<li>在新的窗口或Tab中访问Closure Compiler UI：<a href="http://closure-compiler.appspot.com" target="_blank">http://closure-compiler.appspot.com</a></li>
<li>你能看到预制了一个简单的Hello World函数的Closure Compiler UI。<br/><img style="width:620px;" src="http://code.google.com/closure/compiler/docs/images/getting_started_UI.png" alt="UI screenshot" /></li>
<li>点击“Compile”查看结果：<br/><img style="width:620px;" src="http://code.google.com/closure/compiler/docs/images/getting_started_UI_result.png" alt="UI screenshot" /></li>
</ul>
<p>仅此而已！现在你有了一个函数名和源代码一样但体积小了许多的JavaScript代码。Closure Compiler service通过删除注释、空格和重命名基<span id="more-235"></span>符号等方法将代码字节数从92减少到55。</p>
<p>为了让你更方便，Closure Compiler service将代码输出为default.js并在服务器上保存一小时。你能够从上面的输出面板中拷贝default.js链接的地址并直接访问这个文件。如果你在一个小时内修改了原始JavaScript代码并重新编译，只要你不修改文本域顶部的@output_file_name参数，那么Closure Compiler service会用编译后的新代码覆盖之前的输出文件。利用这个特性，你就可以在测试环境下，通过在网页上直接引入JS文件的方法，快速测试代码。（请不要在生产环境中直接引用该文件）</p>
<p>注意：为了防止被滥用，Closure Compiler service会限制连续使用代码编译的次数。当你遇上系统提示“Too many compiles performed recently”时，这就意味着你暂时超过了这个限制的次数，请稍后再试。</p>
<p>优化JavaScript文件</p>
<p>Closure Compiler service可以优化一个或多个JavaScript文件。</p>
<ol>
<li>将http://code.google.com/closure/compiler/samples/tutorial2.js粘贴到“Add a URL”文本框中。这个文件包含未优化的（在DOM树中创建节点的）代码。</li>
<li>点击“Add”。（如果有多个文件，请重复第一步和第二步，直到你添加完所有的文件）</li>
<li>如果需要指定压缩后的文件名，请修改文本域的顶部的@output_file_name参数。默认的输出文件名是default.js，但你可以根据项目实际情况为它取一个更有意义的名字。再次提醒，服务器对输出文件只保留一个小时。</li>
<li>点击“Compile”。</li>
</ol>
<p>优化后的JavaScript代码显示在下图右手边的面板中。<br/><img style="width:620px;" src="http://code.google.com/closure/compiler/docs/images/getting_started_UI_URL.png" alt="UI screenshot" /></p>
<p>为了使用优化后的代码，你可以复制代码并粘贴到你的源文件中，或者下载该文件到你电脑的适合目录中，或者在script标签中直接链接到该文件（一小时内有效）。</p>
<p>这仅仅是完成了使用Closure Compiler service UI对简单函数的优化例子。如果你希望使用Closure Compiler优化一个大系统代码，你需要直接和Closure Compiler service API对话。相关的更多内容请看《Closure Compiler Service API》</p>
<p>原文地址：http://code.google.com/closure/compiler/docs/gettingstarted_ui.html</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/04/01/closure-compiler-service/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Closure Compiler介绍</title>
		<link>http://blog.tugai.net/2010/04/01/closure-compiler-introduction/</link>
		<comments>http://blog.tugai.net/2010/04/01/closure-compiler-introduction/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 16:34:42 +0000</pubDate>
		<dc:creator>西风瘦马</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[Closure Compiler]]></category>

		<guid isPermaLink="false">http://blog.tugai.net/?p=231</guid>
		<description><![CDATA[什么是Closure Compiler？
Closure Compiler是一个让JavaScript下载和运行加速的工具。它是一个真正的JavaScript编译器。它不是将源代码编译成机器码，而是将源代码编译成更优质的JavaScript代码。它解析、分析JavaScript代码，移除无用的代码，使代码最小化。它还会检查代码语法、变量引用、类型，对JavaScript的常见缺点发出告警。
为了便于调试编译后的代码，你可以安装一个基于Firebug插件的Closure分析器（Closure Inspector），安装后就能使用Firebug方便的查看编译器的输出。
Closure Compiler也能和另一个网页性能评估工具——Page Speed结合。

如何使用Closure Compiler？
可以通过以下几种方法：

一个可在命令行下运行的开源Java应用程序。
一个简单的Web应用程序。
一个RESTful API。

能从Closure Compiler中获益什么？

效率。Closure Compiler会减少JavaScript代码的体积，并让代码更有效。使JS文件加载更快，减少网络带宽消耗。
代码检查。Closure Compiler会对非法的JavaScript和潜在的危险操作提出告警，有助于你生成低bug、易于维护的代码。

原文地址：http://code.google.com/closure/compiler/
]]></description>
			<content:encoded><![CDATA[<h2>什么是Closure Compiler？</h2>
<p>Closure Compiler是一个让JavaScript下载和运行加速的工具。它是一个真正的JavaScript编译器。它不是将源代码编译成机器码，而是将源代码编译成更优质的JavaScript代码。它解析、分析JavaScript代码，移除无用的代码，使代码最小化。它还会检查代码语法、变量引用、类型，对JavaScript的常见缺点发出告警。</p>
<p>为了便于调试编译后的代码，你可以安装一个基于Firebug插件的Closure分析器（Closure Inspector），安装后就能使用Firebug方便的查看编译器的输出。</p>
<p>Closure Compiler也能和另一个网页性能评估工具——Page Speed结合。</p>
<p><span id="more-231"></span></p>
<h2>如何使用Closure Compiler？</h2>
<p>可以通过以下几种方法：</p>
<ul>
<li>一个可在命令行下运行的开源Java应用程序。</li>
<li>一个简单的Web应用程序。</li>
<li>一个RESTful API。</li>
</ul>
<h2>能从Closure Compiler中获益什么？</h2>
<ul>
<li><b>效率。</b><br/>Closure Compiler会减少JavaScript代码的体积，并让代码更有效。使JS文件加载更快，减少网络带宽消耗。</li>
<li><b>代码检查。</b><br/>Closure Compiler会对非法的JavaScript和潜在的危险操作提出告警，有助于你生成低bug、易于维护的代码。</li>
</ul>
<p>原文地址：http://code.google.com/closure/compiler/</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tugai.net/2010/04/01/closure-compiler-introduction/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
