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

<channel>
	<title>BLACKBING &#187; Javascript Core</title>
	<atom:link href="http://blog.blackbing.net/category/javascript%e7%b7%b4%e5%8a%9f%e6%88%bf/javascript-core/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.blackbing.net</link>
	<description>紀錄一些生活或工作的雜事，偶爾會寫一些前端開發的心得</description>
	<lastBuildDate>Sun, 15 Aug 2010 06:24:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Javascript Debug</title>
		<link>http://blog.blackbing.net/2009/08/31/javascript-debug/</link>
		<comments>http://blog.blackbing.net/2009/08/31/javascript-debug/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 13:42:12 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Javascript練功房]]></category>

		<guid isPermaLink="false">http://blog.blackbing.net/?p=402</guid>
		<description><![CDATA[工具 firefox: firebug：强大的debug工具，有名到不需要再做任何介紹了吧！ Leak Monitor：監視firefox 下Memory Leak的工具。 JSView：檢視外部參考的JS和CSS原始碼，動態引入的檔案也會出現。 Clear Cache Button：清除緩存按鈕，開發中有時會被browser快取，每次都要點來點去清cache很麻煩，用這個小按鈕直接清cache就行了。 YSlow：網站效能測試工具，主要是提出了網頁效能的測試標準，並以此標準來評分，在開發網站時可以作為效能參考的依據。 HTML VALIDATOR ：檢測HTML/XHTML的工具，檢查HTML是否有錯誤，例如有標籤沒有正常閉合之類。 IE: firebuglite ：要在firefox以外的瀏覽器使用firebug就是這個了！ IEDevToolBar ：微軟官方出的小工具，可以觀察DOM、CSS等頁面元素，缺點是無法像firebug即時修改。 Script Debugger ：微軟官方出的小工具，在IE上遇到javascript的錯誤相當難debug，此時可以利用這個工具來幫助偵錯，安裝之後需要到工具&#62;選項&#62;進階，將「停用指令碼除錯」取消。 debugBar：類似IEDevToolBar，不過內建一些檢色器之類的小工具，對設計人員來說蠻方便的，推薦安裝。 Drip：偵測IE中Memory Leak的軟體。 IETester：可以同時在windowsXP上開啟IE8 beta 2, IE7 IE 6 and IE5.5，你不用再裝好幾個IE了。 Companion.JS：類似firebug console的工具，主要是可以利用他來做javascript的偵錯，也支援像firebug的console.log。 fiddler：可以擷取所有 HTTP 的封包，點此、還有這、有中文的詳細介紹。 Opera: Web Developer Toolbar and Menu：Opera 是個很特別的瀏覽器，他可以運作在很多意想不到的平台上並且相當流暢，例如wii、window mobile等各種不同裝置上，雖然一般PC的市占率不高，但他的重要性仍然不容小覷。此工具有很多特異功能，例如模擬手持、投影、和電視等設備。但一般 在觀察當前頁面DOM元素等常用的功能稍嫌不足。 Web Accessibility Toolbar：這個工具就相當於firefox裡頭的firebug了。 Safari： Debug Mode on [...]]]></description>
			<content:encoded><![CDATA[<div>
<div><ol>
	<li>工具<ol>
	<li><span style="color: #000000;"><strong>firefox</strong>:</span><ol>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cHM6Ly9hZGRvbnMubW96aWxsYS5vcmcvemgtVFcvZmlyZWZveC9hZGRvbi8xODQz">firebug</a>：强大的debug工具，有名到不需要再做任何介紹了吧！</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cHM6Ly9hZGRvbnMubW96aWxsYS5vcmcvemgtVFcvZmlyZWZveC9hZGRvbi8yNDkw">Leak Monitor</a>：監視firefox 下Memory Leak的工具。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5nb29nbGUuY29tLnR3L3VybD9zYT10JmFtcDtzb3VyY2U9d2ViJmFtcDtjdD1yZXMmYW1wO2NkPTEmYW1wO3VybD1odHRwcyUzQSUyRiUyRmFkZG9ucy5tb3ppbGxhLm9yZyUyRmVuLVVTJTJGZmlyZWZveCUyRmFkZG9uJTJGMjA3NiZhbXA7ZWk9bXBuT1NMQzFNcFBrc2dLSjZieThDZyZhbXA7dXNnPUFGUWpDTkUyLWFSeXZfcmhfMVo1VklrMlZHeXk5QmtBcmcmYW1wO3NpZzI9b21GNXo0ejFkdTFSY2VhMk5XOFpNQQ==">JSView</a>：檢視外部參考的JS和CSS原始碼，動態引入的檔案也會出現。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5nb29nbGUuY29tLnR3L3VybD9zYT10JmFtcDtzb3VyY2U9d2ViJmFtcDtjdD1yZXMmYW1wO2NkPTEmYW1wO3VybD1odHRwcyUzQSUyRiUyRmFkZG9ucy5tb3ppbGxhLm9yZyUyRmZpcmVmb3glMkZhZGRvbiUyRjE4MDEmYW1wO2VpPV9Kbk9TUHZNTnFXc3N3TG4yWjIwQ2cmYW1wO3VzZz1BRlFqQ05GSUtxLUFRdWJhS3phRVBvS1ViYU8wenk2RUtnJmFtcDtzaWcyPVRlSU5FUmNyNldXQi1Rd3g5V1ZXWXc=">Clear Cache Button</a>：清除緩存按鈕，開發中有時會被browser快取，每次都要點來點去清cache很麻煩，用這個小按鈕直接清cache就行了。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5nb29nbGUuY29tLnR3L3VybD9zYT10JmFtcDtzb3VyY2U9d2ViJmFtcDtjdD1yZXMmYW1wO2NkPTMmYW1wO3VybD1odHRwcyUzQSUyRiUyRmFkZG9ucy5tb3ppbGxhLm9yZyUyRmVuLVVTJTJGZmlyZWZveCUyRmFkZG9uJTJGNTM2OSZhbXA7ZWk9WEpyT1NMRzhLSk9Lc3dLMXJlVzRDZyZhbXA7dXNnPUFGUWpDTkhrYmJaOVJ5WG9ONlQ0Y294TWxxaXFhcC1HSFEmYW1wO3NpZzI9b0FQVkVvXzVhRTRQNEl1a1l3NlRlZw==">YSlow</a>：網站效能測試工具，主要是提出了網頁效能的測試標準，並以此標準來評分，在開發網站時可以作為效能參考的依據。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzZXJzLnNreW5ldC5iZS9tZ3VldXJ5L21vemlsbGEv">HTML VALIDATOR </a>：檢測HTML/XHTML的工具，檢查HTML是否有錯誤，例如有標籤沒有正常閉合之類。</li>
</ol></li>
	<li><strong>IE</strong>:<ol>
	<li><a title=\"firebuglite\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2dldGZpcmVidWcuY29tL2xpdGUuaHRtbA==">firebuglite</a> ：要在firefox以外的瀏覽器使用firebug就是這個了！</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5taWNyb3NvZnQuY29tL2Rvd25sb2FkUy9kZXRhaWxzLmFzcHg/ZmFtaWx5aWQ9RTU5QzM5NjQtNjcyRC00NTExLUJCM0UtMkQ1RTFEQjkxMDM4JmFtcDtkaXNwbGF5bGFuZz1lbg==">IEDevToolBar</a> ：微軟官方出的小工具，可以觀察DOM、CSS等頁面元素，缺點是無法像firebug即時修改。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cHM6Ly93d3cubWljcm9zb2Z0LmNvbS9kb3dubG9hZHMvZGV0YWlscy5hc3B4P0ZhbWlseUlEPTJiMmE4ZjVhLTk0ZjAtNDk5Ny05NDY3LTUzMWZhZjBhMzIyMyZhbXA7ZGlzcGxheWxhbmc9emgtdHc=">Script Debugger</a> ：微軟官方出的小工具，在IE上遇到javascript的錯誤相當難debug，此時可以利用這個工具來幫助偵錯，安裝之後需要到工具&gt;選項&gt;進階，將「停用指令碼除錯」取消。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5teS1kZWJ1Z2Jhci5jb20vd2lraS9Eb2MvRGVidWdiYXJJbnN0YWxs">debugBar</a>：類似IEDevToolBar，不過內建一些檢色器之類的小工具，對設計人員來說蠻方便的，推薦安裝。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5vdXRvZmhhbndlbGwuY29tL2llbGVhay9pbmRleC5waHA/dGl0bGU9TWFpbl9QYWdl">Drip</a>：偵測IE中Memory Leak的軟體。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5teS1kZWJ1Z2Jhci5jb20vd2lraS9JRVRlc3Rlci9Ib21lUGFnZQ==">IETester</a>：可以同時在windowsXP上開啟IE8 beta 2, IE7 IE 6 and IE5.5，你不用再裝好幾個IE了。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5teS1kZWJ1Z2Jhci5jb20vd2lraS9Db21wYW5pb25KUy9Ib21lUGFnZQ==">Companion.JS</a>：類似firebug console的工具，主要是可以利用他來做javascript的偵錯，也支援像firebug的console.log。</li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5maWRkbGVyMi5jb20vZmlkZGxlcjIv">fiddler</a>：可以擷取所有 HTTP 的封包，<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Jsb2cuc2hhbmdlci5uZXQvYXJ0aWNsZS5hc3A/aWQ9MjQ2">點此</a>、<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Jsb2cubWluaWFzcC5jb20vcG9zdC8yMDA4LzA4L1VzaW5nLUZpZGRsZXItdG8tVGFtcGVyLUhUVFAtUmVzcG9uc2UtQ29udGVudC5hc3B4">還有這</a>、有中文的詳細介紹。 </li>
</ol></li>
	<li><strong>Opera</strong>:<ol>
	<li><a id=\"g4oc\" title=\"Web Developer Toolbar and Menu\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL29wZXJhd2lraS5pbmZvL1dlYkRldlRvb2xiYXI=" target=\"_blank\">Web Developer Toolbar and Menu</a>：Opera 是個很特別的瀏覽器，他可以運作在很多意想不到的平台上並且相當流暢，例如wii、window mobile等各種不同裝置上，雖然一般PC的市占率不高，但他的重要性仍然不容小覷。此工具有很多特異功能，例如模擬手持、投影、和電視等設備。但一般 在觀察當前頁面DOM元素等常用的功能稍嫌不足。</li>
	<li><a id=\"n3nl\" title=\"Web Accessibility Toolbar\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5wYWNpZWxsb2dyb3VwLmNvbS9yZXNvdXJjZXMvd2F0LWFib3V0Lmh0bWw=" target=\"_blank\">Web Accessibility Toolbar</a>：這個工具就相當於firefox裡頭的firebug了。</li>
</ol></li>
	<li><strong>Safari</strong>：<ol>
	<li><a id=\"hk-.\" title=\"Debug Mode on Safari4\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JvYnJvaGFuLmNvbS8yMDA3LzA2LzExL2VuYWJsZS1kZWJ1Zy1tb2RlLW9uLXNhZmFyaS13aW5kb3dzLWVycm9yLw==" target=\"_blank\">Debug Mode on Safari4</a>：safari4問世之後，由於他神速的運行javascript而獲得廣大的迴響，而safari4也內建開發人員工具，但是要啟用，啟用方法請參考：<a id=\"xnqi\" title=\"Enable Debug Mode on Safari (Windows) + Error\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JvYnJvaGFuLmNvbS8yMDA3LzA2LzExL2VuYWJsZS1kZWJ1Zy1tb2RlLW9uLXNhZmFyaS13aW5kb3dzLWVycm9yLw==" target=\"_blank\">Enable Debug Mode on Safari (Windows) + Error</a></li>
</ol></li>
</ol></li>
	<li>你真的會用firebug？<ol>
	<li>console：主控台可以顯示所有進行的post、get請求，可以查看每一個請求的header、參數、response。<br />
<ol>
	<li>console.log：請別再用alert來debug了。透過console.log可以查看物件「脫光」物件的所有屬性或方法。</li>
	<li>勘查：主控台的一個特異功能，若你發現頁面運行的速度或效能很差，你可以打開「勘查」來勘查所有被呼叫運行的function有哪些，並且會列出某個function被呼叫幾次、時間、等等。在觀察效能時非常有用。</li>
</ol></li>
	<li>HTML：firebug 超好用的其中一點就在於他這個所見即所得真是做的完美無缺，你可以直接查看當前文檔的css、也可以直接編輯，所見即所得，更可以編輯、刪除任意元素，或 者直接查看某元素的DOM物件。而且CSS被覆蓋的順序也會一層層被列出，在觀察CSS style時絕對是不可或缺的工具。</li>
	<li>CSS：列出所有目前正在運行的CSS。</li>
	<li>script：列出所有目前正在運行的javascript。<ol>
	<li>就算script被壓縮、編碼過，client端終究得解譯回來，通常編譯過之後的程式都需要透過eval來解譯回來，透過script工具便可以知道被eval起來的函數如何被解譯，編碼過後的程式完全一目了然。</li>
	<li>監看表達式可以查出某個被運行的function被執行，並且可以設定中斷點來逐步檢查，並查看stack。</li>
</ol></li>
	<li>DOM：查看任意元素、物件內的所有成員屬性。</li>
	<li>NET：所有任何http協定的東西都會列在NET裡頭，你可以透過tab來切換欲觀察的傳輸。其中XHR就是查看透過ajax請求的數據資料。</li>
</ol></li>
	<li>理解arguments.callee<ol>
	<li>arguments.callee指向參數的function。</li>
	<li>arguments.callee.caller指向被誰呼叫，在不知道某function被誰呼叫而引發錯誤時可以用來查出被誰調用而導致錯誤產生。</li>
</ol></li>
</ol></div>
</div> <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=402" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2009/08/31/javascript-debug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is &quot;this&quot; in Javascript（上）</title>
		<link>http://blog.blackbing.net/2009/08/02/what-is-this-in-javascripta/</link>
		<comments>http://blog.blackbing.net/2009/08/02/what-is-this-in-javascripta/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 09:15:06 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Javascript練功房]]></category>
		<category><![CDATA[未分類]]></category>

		<guid isPermaLink="false">http://blog.blackbing.net/?p=346</guid>
		<description><![CDATA[很多人搞不懂Javascript的「this」這個關鍵字，或者是覺得懂，但是有些地方的this又覺得怪怪的。看過一些this的教學，我覺得都講得太複雜了，其實「this」只有一個重點而已。 this永遠指向「被執行時的作用域」 蝦毁？？被執行？作用域？？沒關係，試著把自己當作是編譯器，只要你不會在自己所撰寫的程式中迷路，其實this一點都不難區分。讓我們來看幾個例子： var t = this;console.log(t); //window function whatIsThis(){ console.log(this); } whatIsThis(); //window 變數t指向this，作用域在window底下，因此this指向window，這沒有問題，function whatIsThis裡頭印出的this指向誰？別會錯意囉，this永遠指向被「誰」呼叫，這個function是在window的作用域下被呼叫的，因此指向window，同理，我們來看下個例子： function wrapper(){ whatIsThis(); } wrapper(); //window 什麼？我是透過wrapper這個function來呼叫whatIsThis的，為什麼還是window？請想想wrapper的作用域是誰？是 window，window呼叫wrapper，this還是指向window，作用域並沒有改變，這時呼叫whatIsThis當然作用域還是沒有改變，依然指向window。 接下來結合其他的型態： var arr = [this, 1, 2, function(){console.log(this)}]; console.log(arr[0]); //window arr[3](); //arr arr[0]指向window，原因是arr這個陣列被宣告時作用域是在window底下的，因此arr[0]是指向this。而arr[3]呢？當他被執行時，this被closure的方式被保存在function裡頭，因此呼叫arr[3]時，this指向他當時的作用域，也就是arr，看起來有點複雜，不過只要把自己想程式編譯器，問自己this何時被執行就好了。 Object跟Array一樣，其實大部分應該會更常在Object裡頭來使用this，我們來看一個比較實用的例子： var box = { length:100, width: 300, area: this.length* this.width }; console.log(box.area); 看起來蠻合邏輯的一段程式，猜猜看，box.area是什麼？答案是NaN。Why?因為this根本就不是指向box，哪來的 this.length、this.width。剛說過this永遠指向「被執行的作用域」，box物件在宣告時，屬性volumn被指派時，this當然是指向window，所以這個邏輯是錯的。那怎辦？很多人遇到這個問題時，「那就不要使用this吧！」所以程式變成 var box = [...]]]></description>
			<content:encoded><![CDATA[很多人搞不懂Javascript的「this」這個關鍵字，或者是覺得懂，但是有些地方的this又覺得怪怪的。看過一些this的教學，我覺得都講得太複雜了，其實「this」只有一個重點而已。

this永遠指向「<strong>被執行時的作用域</strong>」

蝦毁？？被執行？作用域？？沒關係，試著把自己當作是編譯器，只要你不會在自己所撰寫的程式中迷路，其實this一點都不難區分。讓我們來看幾個例子：<span id="more-346"></span>

<pre class="brush: jscript;">
 var t = this;console.log(t); //window
 function whatIsThis(){
     console.log(this); 
}
whatIsThis(); //window
 </pre>

變數t指向this，作用域在window底下，因此this指向window，這沒有問題，function whatIsThis裡頭印出的this指向誰？別會錯意囉，this永遠指向被「誰」呼叫，這個function是在window的作用域下被呼叫的，因此指向window，同理，我們來看下個例子：

<pre class="brush: jscript;">
 function wrapper(){
     whatIsThis();
 }
wrapper(); //window
 </pre>

什麼？我是透過wrapper這個function來呼叫whatIsThis的，為什麼還是window？請想想wrapper的作用域是誰？是 window，window呼叫wrapper，this還是指向window，作用域並沒有改變，這時呼叫whatIsThis當然作用域還是沒有改變，依然指向window。

接下來結合其他的型態：

<pre class="brush: jscript;">
 var arr = [this, 1, 2, function(){console.log(this)}];
 console.log(arr[0]); //window
 arr[3](); //arr
 </pre>

arr[0]指向window，原因是arr這個陣列被宣告時作用域是在window底下的，因此arr[0]是指向this。而arr[3]呢？當他被執行時，this被closure的方式被保存在function裡頭，因此呼叫arr[3]時，this指向他當時的作用域，也就是arr，看起來有點複雜，不過只要把自己想程式編譯器，問自己this何時被執行就好了。 Object跟Array一樣，其實大部分應該會更常在Object裡頭來使用this，我們來看一個比較實用的例子：

<pre class="brush: jscript;">
 var box = {
     length:100,
     width: 300,
     area: this.length* this.width
 };
 console.log(box.area);
 </pre>

看起來蠻合邏輯的一段程式，猜猜看，box.area是什麼？答案是NaN。Why?因為this根本就不是指向box，哪來的 this.length、this.width。剛說過this永遠指向「被執行的作用域」，box物件在宣告時，屬性volumn被指派時，this當然是指向window，所以這個邏輯是錯的。那怎辦？很多人遇到這個問題時，「那就不要使用this吧！」所以程式變成

<pre class="brush: jscript;">
 var box = {
     length:100,
     width: 300,
     area: box.length* box.width
 };
 console.log(box.area);
 </pre>

OK！box.area很正確的被計算出來了，但是這是個好辦法嗎？假如今天程式變成

<pre class="brush: jscript;">
 var box = {
     length:100,
     width: 300,
     area: box.length* box.width,
     volumn: box.length* box.width * box.height,
     height:50
};
 console.log('box.volumn='+box.volumn);
 </pre>

再猜猜看，box.volumn的值是什麼？答案是&#8230;..NaN，因為box在被宣告時，volumn屬性決定前尚未宣告height，導致 box.height變成undefined。「那就把height提前宣告就好啦？」等等，這樣真是不求甚解。這樣的需求用this的特性來取得是很直覺的事情，應該要想辦法搞定的。

<pre class="brush: jscript;">
 var box = {
     length:100,
     width: 300,
     getArea: function(){ return this.length* this.width},
     getVolumn: function(){ return this.length* this.width*this.height},
     height:50
 };
 console.log('box.volumn='+box.getVolumn());
 </pre>

如上，用getArea的function來動態取得box的長寬高，box.getVolumn function裡頭的this便是指向box這個物件，因為getVolumn被呼叫時，作用域就在box，所以this指向box。 this跟幾個重要型態的關係大概就是這樣，萬變不離其宗，在任何型態裡頭使用this都是照這個思維去走即可。下一篇會再介紹new Object之後的this以及與window事件交互作用之後的this。 <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=346" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2009/08/02/what-is-this-in-javascripta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ie6 Memory Leak的解決方式</title>
		<link>http://blog.blackbing.net/2009/07/31/ie6-memory-leak%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e5%bc%8f/</link>
		<comments>http://blog.blackbing.net/2009/07/31/ie6-memory-leak%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e5%bc%8f/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 15:13:38 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Javascript練功房]]></category>
		<category><![CDATA[Programing]]></category>

		<guid isPermaLink="false">http://blog.blackbing.net/?p=383</guid>
		<description><![CDATA[前端程式越搞越複雜，接踵而至的問題越來越多，雖然瀏覽器的效能不斷地提昇，但是只要IE6沒被淘汰的一天，網頁程式設計師永遠要替IE6補洞，Memory Leak是一個非常難搞得議題。關於微軟的官方說法可以參考Understanding and Solving Internet Explorer Leak Patterns，裡頭有詳細說明IE6會如何發生memory leak的情況。發生memory leak的徵狀就是：記憶體不斷飆上去，就算reload頁面，記憶體也不會被釋放，除非關掉瀏覽器。 在此不介紹為何為引發memory leak，因為我覺得這是瀏覽器應該要處理的問題，要拿出來講只是因為IE6沒辦法處理好罷了，雖然其他瀏覽器也並不是沒有這個問題，但是至少問題都沒有這麼嚴重，因此只需要針對IE6來處理即可。以下是我整理出幾種比較好的處理方式： Douglas Crockford針對memory leak的議題發表一篇文章：JScript Memory Leaks提出一個purge的function，概念非常簡單，就是遍歷元素底下的所有節點，將所有的function設為null（這屬於closure的循環參照索引發的memory leak）。一般來說可以在unload時或者你要removeNode之前來做這個動作，例如： function purge(d) { var a = d.attributes, i, l, n; if (a) { l = a.length; for (i = 0; i &#60; l; i += 1) { n = a[i].name; if (typeof d[n] === 'function') { d[n] [...]]]></description>
			<content:encoded><![CDATA[<p>前端程式越搞越複雜，接踵而至的問題越來越多，雖然瀏覽器的效能不斷地提昇，但是只要IE6沒被淘汰的一天，網頁程式設計師永遠要替IE6補洞，Memory Leak是一個非常難搞得議題。關於微軟的官方說法可以參考<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL21zZG4ubWljcm9zb2Z0LmNvbS9lbi11cy9saWJyYXJ5L2JiMjUwNDQ4KFZTLjg1KS5hc3B4" target=\"_blank\">Understanding and Solving Internet Explorer Leak Patterns</a>，裡頭有詳細說明IE6會如何發生memory leak的情況。發生memory leak的徵狀就是：記憶體不斷飆上去，就算reload頁面，記憶體也不會被釋放，除非關掉瀏覽器。</p>

<p>在此不介紹為何為引發memory leak，因為我覺得這是瀏覽器應該要處理的問題，要拿出來講只是因為IE6沒辦法處理好罷了，雖然其他瀏覽器也並不是沒有這個問題，但是至少問題都沒有這麼嚴重，因此只需要針對IE6來處理即可。以下是我整理出幾種比較好的處理方式：<span id="more-383"></span></p>

<ol>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2phdmFzY3JpcHQuY3JvY2tmb3JkLmNvbQ==" target=\"_blank\">Douglas Crockford</a>針對memory leak的議題發表一篇文章：<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2phdmFzY3JpcHQuY3JvY2tmb3JkLmNvbS9tZW1vcnkvbGVhay5odG1s" target=\"_blank\">JScript Memory Leaks</a>提出一個purge的function，概念非常簡單，就是遍歷元素底下的所有節點，將所有的function設為null（這屬於closure的循環參照索引發的memory leak）。一般來說可以在unload時或者你要removeNode之前來做這個動作，例如：  <pre class="brush: jscript;">
function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i &lt; l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i &lt; l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}
window.onunload = function(){
    purge(document.body);
}
</pre>
 </li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL25vdmVtYmVyYm9ybi5uZXQvamF2YXNjcmlwdC9ldmVudC1jYWNoZQ==" target=\"_blank\">Event Cache</a> ：若你的專案沒用到一些framework時，可能會用到一些自訂的addEvent方法，這時可以透過EventCache.add來將事件加入cache，然後在unload時做EventCache.flush。這樣可以避免在unload時循環整個body，雖然降低了memory leak，但卻吃掉cpu的效能。<pre class="brush: jscript;">
var EventCache = function(){
 var listEvents = [];&lt;/p&gt;
&lt;p&gt; return {
 listEvents : listEvents,&lt;/p&gt;
&lt;p&gt; add : function(node, sEventName, fHandler, bCapture){
 listEvents.push(arguments);
 },&lt;/p&gt;
&lt;p&gt; flush : function(){
 var i, item;
 for(i = listEvents.length - 1; i &gt;= 0; i = i - 1){
 item = listEvents[i];&lt;/p&gt;
&lt;p&gt; if(item[0].removeEventListener){
 item[0].removeEventListener(item[1], item[2], item[3]);
 };&lt;/p&gt;
&lt;p&gt; /* From this point on we need the event names to be prefixed with 'on&quot; */
 if(item[1].substring(0, 2) != &quot;on&quot;){
 item[1] = &quot;on&quot; + item[1];
 };&lt;/p&gt;
&lt;p&gt; if(item[0].detachEvent){
 item[0].detachEvent(item[1], item[2]);
 };&lt;/p&gt;
&lt;p&gt; item[0][item[1]] = null;
 };
 }
 };
}();
</pre>
 </li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5oZWRnZXJ3b3cuY29tLzM2MC9kaHRtbC9pZTZfbWVtb3J5X2xlYWtfZml4Lw==" target=\"_blank\">利用try&#8230;finally來清除不被引用的參考</a>：這常會發生在我們利用function來creat Element時，忽略了不再被引用的參考，然後直接return，在IE6中會引發memory leak，這篇文章透過try,finally來清除不被引用的參考，例如： <pre class="brush: jscript;">
function createButton() {
      var obj = document.createElement(&quot;button&quot;);
      obj.innerHTML = &quot;click me&quot;;
      obj.onclick = function() {
        //handle onclick
      }
      obj.onmouseover = function() {
        //handle onmouseover
      }&lt;/p&gt;
&lt;p&gt;      //this helps to fix the memory leak issue
      try {
        return obj;&lt;/p&gt;
&lt;p&gt;      } finally {
        obj = null;
      }
    }
</pre>
 </li>
	<li><a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5qdWxpZW5sZWNvbXRlLm5ldC9ibG9nLzIwMDcvMTIvMzgv">innerHTML所引發的memory leak</a>：這個議題最為常見，innerHTML不被W3C列為標準，但因為他比起操作DOM元素容易的多了，因此廣泛的被使用，但是之前有提到，最好是在removeNode時做一次檢查，將node裡頭的function全部設為null，以避免closure所引發的memory leak，但是innerHTML實在太方便了，讓所有programer將這個問題拋在腦後，因為我隨時可以設定某個元素的innerHTML，哪怕他裡頭又肥又大，一句innerHTML就可以改變DOM元素。文章裡頭的例子用YUI來做示範，不過事實上，他還是用了Douglas了perge來清除循環引用。 <pre class="brush: jscript;">
YAHOO.util.Dom.setInnerHTML = function (el, html) {
    el = YAHOO.util.Dom.get(el);
    if (!el || typeof html !== 'string') {
        return null;
    }&lt;/p&gt;
&lt;p&gt;    // Break circular references.
    (function (o) {&lt;/p&gt;
&lt;p&gt;        var a = o.attributes, i, l, n, c;
        if (a) {
            l = a.length;
            for (i = 0; i &lt; l; i += 1) {
                n = a[i].name;
                if (typeof o[n] === 'function') {
                    o[n] = null;
                }
            }
        }&lt;/p&gt;
&lt;p&gt;        a = o.childNodes;&lt;/p&gt;
&lt;p&gt;        if (a) {
            l = a.length;
            for (i = 0; i &lt; l; i += 1) {
                c = o.childNodes[i];&lt;/p&gt;
&lt;p&gt;                // Purge child nodes.
                arguments.callee(c);&lt;/p&gt;
&lt;p&gt;                // Removes all listeners attached to the element via YUI's addListener.
                YAHOO.util.Event.purgeElement(c);
            }
        }&lt;/p&gt;
&lt;p&gt;    })(el);&lt;/p&gt;
&lt;p&gt;    // Remove scripts from HTML string, and set innerHTML property
    el.innerHTML = html.replace(/&lt;script[^&gt;]*&gt;[\S\s]*?&lt;\/script[^&gt;]*&gt;/ig, &quot;&quot;);&lt;/p&gt;
&lt;p&gt;    // Return a reference to the first child
    return el.firstChild;
};
</pre>
 </li>
</ol>

<p>大概是這樣了，memory leak實在是個很惱人的議題，不過有責任感的前端工程是還是要將之視為己任的，畢竟項目越做越大，然而記憶體越吃越兇，這是很可怕的一件事。</p> <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=383" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2009/07/31/ie6-memory-leak%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>取得目前的Time stamp，以及「+」運算符的特性</title>
		<link>http://blog.blackbing.net/2009/05/01/%e5%8f%96%e5%be%97%e7%9b%ae%e5%89%8d%e7%9a%84time-stamp/</link>
		<comments>http://blog.blackbing.net/2009/05/01/%e5%8f%96%e5%be%97%e7%9b%ae%e5%89%8d%e7%9a%84time-stamp/#comments</comments>
		<pubDate>Fri, 01 May 2009 11:34:48 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Javascript練功房]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.blackbing.net/?p=322</guid>
		<description><![CDATA[一般來說，我們都會用 new Date().getTime() 來取得目前的time stamp，今天在cssRain看到JavaScript&#8212;new Date().getTime()的简写方式，發現 +new Date() 這兩個會出現相同的結果，這讓我不禁好奇了「+」這個運算符他的特殊用法， 測試了幾個結果： console.log(+&#34;01&#34;);//1 console.log(+&#34;100&#34;);//100 console.log(+&#34;1px&#34;);//NaN console.log(+&#34;abc&#34;);//NaN console.log(+&#34;abc&#34;);//NaN console.log(+{obj:'obj'});//NaN console.log(+['array']);//NaN console.log(+true);//1 console.log(+false);//0 OK~「+」運算符的功能就是像new Number()的結果，因此 new Number(new Date()) 也是跟 +new Date() 是一樣的結果 結論： +運算符放在任何變數前面，會將該變數轉成number型態，但跟parseInt()是不同的(parseInt(&#8217;1px&#8217;) = 1)。 以下結果相同： new Date().getTime() new Number(new Date()) +new Date()]]></description>
			<content:encoded><![CDATA[一般來說，我們都會用

<pre class="brush: jscript;">
new Date().getTime()
</pre>

來取得目前的time stamp，今天在<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5jc3NyYWluLmNu">cssRain</a>看到<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5jc3NyYWluLmNuL2FydGljbGUuYXNwP2lkPTExMTY=" target=\"_blank\">JavaScript&#8212;new Date().getTime()的简写方式</a>，發現

<pre class="brush: jscript;">
+new Date()
</pre>

這兩個會出現相同的結果，這讓我不禁好奇了「+」這個運算符他的特殊用法，
測試了幾個結果：<span id="more-322"></span>

<pre class="brush: jscript;">
console.log(+&quot;01&quot;);//1
console.log(+&quot;100&quot;);//100
console.log(+&quot;1px&quot;);//NaN
console.log(+&quot;abc&quot;);//NaN
console.log(+&quot;abc&quot;);//NaN
console.log(+{obj:'obj'});//NaN
console.log(+['array']);//NaN
console.log(+true);//1
console.log(+false);//0
</pre>

OK~「+」運算符的功能就是像new Number()的結果，因此
new Number(new Date()) 也是跟
+new Date() 是一樣的結果

結論：
<ol>
	<li>+運算符放在任何變數前面，會將該變數轉成number型態，但跟parseInt()是不同的(parseInt(&#8217;1px&#8217;) = 1)。</li>
	<li>以下結果相同：
<ol>
	<li>new Date().getTime()</li>
	<li>new Number(new Date())</li>
	<li>+new Date()</li>
</ol>
</li>
</ol> <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=322" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2009/05/01/%e5%8f%96%e5%be%97%e7%9b%ae%e5%89%8d%e7%9a%84time-stamp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>取得script的Root路徑</title>
		<link>http://blog.blackbing.net/2009/05/01/%e5%8f%96%e5%be%97script%e7%9a%84root%e8%b7%af%e5%be%91/</link>
		<comments>http://blog.blackbing.net/2009/05/01/%e5%8f%96%e5%be%97script%e7%9a%84root%e8%b7%af%e5%be%91/#comments</comments>
		<pubDate>Fri, 01 May 2009 10:26:56 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Javascript練功房]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.blackbing.net/?p=319</guid>
		<description><![CDATA[做專案時常常因為路徑問題而困擾，因為現在用的路徑很可能到另一個環境就有大幅度的變動，寫前端程式的時候更常遇到這個問題，當然每個人都會把路徑獨立出來成一個變數，到時候若要改的話只要改個變數即可，但像CSS的url command就可以讀相對路徑，因此路徑只要指向相對路徑即可，就算環境換了也不用改任何程式。因此如果每一隻script都可以知道自己被include的Root路徑，需要指定路徑的地方就只要根據這個Root路徑來做相對路徑即可。例如一隻js檔裡頭需要動態產生圖片，那麼就會需要用到這個相對路徑。 var SCRIPTROOTSRC = (function(){ var RELATIVE = /^[\w\.]+[^:]*$/; function makePath(href, path) { if (RELATIVE.test(href)) href = (path &#124;&#124; &#34;&#34;) + href; return href; }; function getPath(href, path) { href = makePath(href, path); return href.slice(0, href.lastIndexOf(&#34;/&#34;) + 1); }; // get the path to this script var scripts = document.getElementsByTagName('script'); var script = scripts[scripts.length [...]]]></description>
			<content:encoded><![CDATA[做專案時常常因為路徑問題而困擾，因為現在用的路徑很可能到另一個環境就有大幅度的變動，寫前端程式的時候更常遇到這個問題，當然每個人都會把路徑獨立出來成一個變數，到時候若要改的話只要改個變數即可，但像CSS的url command就可以讀相對路徑，因此路徑只要指向相對路徑即可，就算環境換了也不用改任何程式。因此如果每一隻script都可以知道自己被include的Root路徑，需要指定路徑的地方就只要根據這個Root路徑來做相對路徑即可。例如一隻js檔裡頭需要動態產生圖片，那麼就會需要用到這個相對路徑。<span id="more-319"></span>

<pre class="brush: jscript;">
var SCRIPTROOTSRC = (function(){
 var RELATIVE = /^[\w\.]+[^:]*$/;
 function makePath(href, path) {
 if (RELATIVE.test(href)) href = (path || &quot;&quot;) + href;
 return href;
 };

 function getPath(href, path) {
 href = makePath(href, path);
 return href.slice(0, href.lastIndexOf(&quot;/&quot;) + 1);
 };

 // get the path to this script
 var scripts = document.getElementsByTagName('script');
 var script = scripts[scripts.length - 1];
 var path = getPath(script.src);
 return path;
})();
alert(SCRIPTROOTSRC);
</pre>

程式很簡單，只要執行時取得document.getElementsByTagName(&#8216;script&#8217;)的最後一個，就是指向目前這隻script，分析自己的src就會知道路徑，再用Regular Expression來取得root。
既然知道root，就可以用SCRIPTROOTSRC來做相對路徑了。

唯一需要注意的地方，如果有好幾隻script的話，請把SCRIPTROOTSRC包在物件裡頭使用，或是自己取別的名稱吧！</pre> <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=319" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2009/05/01/%e5%8f%96%e5%be%97script%e7%9a%84root%e8%b7%af%e5%be%91/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>撰寫javascript的規範 (Code Conventions for the JavaScript Programming Language)</title>
		<link>http://blog.blackbing.net/2009/01/01/%e6%92%b0%e5%af%abjavascript%e7%9a%84%e8%a6%8f%e7%af%84-code-conventions-for-the-javascript-programming-language/</link>
		<comments>http://blog.blackbing.net/2009/01/01/%e6%92%b0%e5%af%abjavascript%e7%9a%84%e8%a6%8f%e7%af%84-code-conventions-for-the-javascript-programming-language/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 04:21:42 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Javascript練功房]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blackbingo.myftp.org/blog/?p=221</guid>
		<description><![CDATA[javascript程式 javascript程式應該獨立成js檔被include進來，盡量不要將javascript code 與embade在HTML裡頭，這麼一來無法利用cache或壓縮，將來要維護也不容易。 盡量將引入的javascript放到HTML的最後，將會減少載入script的延遲。 script標籤裡頭不需要使用lauguage, type屬性。 縮排 縮排的單位為4個空白，請避免使用tab來做縮排，即使是現在，tab字元到底該空多少空白依然沒有標準，雖然空白會增加檔案大小，但是可以透過Minification的技巧來降低檔案大小。 程式碼行寬 程式碼每行的長度最好不要超過80個字元，下一行應該要縮排8個空白。 註解 不要下白痴註解例如：  i = 0; // Set i to zero.  變數的宣告 所有變數都必需要用var來宣告，未使用var宣告都會被當成全域變數，你一定不希望你的程式有一大堆的全域變數。變數不需要定義初始值，但是最好在定義時給他註解，這是一個好習慣。 var currentEntry; // currently selected table entry var level; // indentation level  var size; // size of table 另外javascript並沒有block scope，只要定義了變數，在function裡頭就可以存取，因此請將定義變數寫在function的最上面。 函式的宣告 函式名稱與&#8217;(&#8216; (左括弧)中間不要有空白，而&#8217;)&#8217; (右括弧)與&#8217;{&#8216; (大括弧)中間空一個空白，例如 function outer(c, d) { }  變數的命名 ☆ [...]]]></description>
			<content:encoded><![CDATA[<strong>javascript程式</strong>
<p style="padding-left: 30px;">javascript程式應該獨立成js檔被include進來，盡量不要將javascript code 與embade在HTML裡頭，這麼一來無法利用cache或壓縮，將來要維護也不容易。
盡量將引入的javascript放到HTML的最後，將會減少載入script的延遲。
script標籤裡頭不需要使用lauguage, type屬性。 <span id="more-221"></span>

<strong>縮排</strong>
<p style="padding-left: 30px;">縮排的單位為4個空白，請避免使用tab來做縮排，即使是現在，tab字元到底該空多少空白依然沒有標準，雖然空白會增加檔案大小，但是可以透過Minification的技巧來降低檔案大小。</p>

<strong>程式碼行寬</strong>
<p style="padding-left: 30px;"><strong></strong>程式碼每行的長度最好不要超過80個字元，下一行應該要縮排8個空白。</p>

<strong>註解</strong>
<p style="padding-left: 30px;"><strong></strong>不要下白痴註解例如：
<pre class="brush: jscript;"> i = 0; // Set i to zero.</pre> 

<strong>變數的宣告</strong>
<p style="padding-left: 30px;">所有變數都必需要用var來宣告，未使用var宣告都會被當成全域變數，你一定不希望你的程式有一大堆的全域變數。變數不需要定義初始值，但是最好在定義時給他註解，這是一個好習慣。
<pre class="brush: jscript;">
var currentEntry; // currently selected table entry
var level;        // indentation level 
var size;         // size of table
</pre>
另外javascript並沒有block scope，只要定義了變數，在function裡頭就可以存取，因此請將定義變數寫在function的最上面。

<strong>函式的宣告</strong>
<p style="padding-left: 30px;">函式名稱與&#8217;(&#8216; (左括弧)中間不要有空白，而&#8217;)&#8217; (右括弧)與&#8217;{&#8216; (大括弧)中間空一個空白，例如
<pre class="brush: jscript;">
function outer(c, d) {
} </pre>

<strong>變數的命名</strong>
<p style="padding-left: 30px;">☆ 只使用大小寫英數字 與&#8217;_'(底線)來命名，避免使用特殊字元來命名。
☆ &#8217;_'(底線)開頭的變數通常表示私有變數，若非此需求請不要如此命名。
☆ 大部分的變數開頭為小寫。 
☆ 需要new 的物件請用大寫字元開頭，需要new function的錯誤非常難發現，開頭大寫是一個較好的撰寫習慣。
☆ 全域變數應該要全部大寫 。
☆ 不要使用無意義的咒罵之詞命名變數，你有可能會惹腦閱讀者。例如：<pre class="brush: jscript;">
var fuckingElement = document.getElementById('fuck');
var myBossIs = 'shit';
</pre>

<strong>敘述</strong>
<p style="padding-left: 30px;"> 每一行必須要由; 結束，雖然javascript允許每一行敘述都可以不需要分號，但這樣可能會引起一些錯誤。</p>

<strong></strong>

<strong>continue</strong>
<p style="padding-left: 30px;"><strong></strong>避免使用continue ，他會讓控制流程模糊。</p>

<strong>with</strong>
<p style="padding-left: 30px;">不要使用with。 </p>

<strong>{} 與 []</strong>
<p style="padding-left: 30px;"><strong></strong>使用{}取代 new Object，使用[]取代new Array()。(例如我之前寫的<a title=\"Permanent Link to new Object() vs {} and new Array() vs [] 效能比較\" rel=\"bookmark\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Jsb2cuYmxhY2tiaW5nLm5ldC8/cD0xNg==">new Object() VS {} AND new Array() VS [] 效能比較</a>)</p>

<strong> === 與 !== </strong>
<p style="padding-left: 30px;"><strong></strong>比起== 與 !=，使用===與!==會更好，特別是你要判斷false變量時。(例如我在<a title=\"Permanent Link to Javascript 型態\" rel=\"bookmark\" href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Jsb2cuYmxhY2tiaW5nLm5ldC8/cD0yMTM=">JAVASCRIPT 型態</a>中的說明) </p>

<strong>使用eval是邪惡的(eval is Evil)</strong>
<p style="padding-left: 30px;"><strong></strong>避免使用eval，另外像Function建構式，傳遞字串到setTimeout, setInterval 執行的方式、在HTML裡頭寫 onclick=""也都是用eval來執行，請避免。</p>

原文連結：<a href="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2phdmFzY3JpcHQuY3JvY2tmb3JkLmNvbS9jb2RlLmh0bWw=">Code Conventions for the JavaScript Programming Language</a> <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=221" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2009/01/01/%e6%92%b0%e5%af%abjavascript%e7%9a%84%e8%a6%8f%e7%af%84-code-conventions-for-the-javascript-programming-language/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript 型態</title>
		<link>http://blog.blackbing.net/2008/12/17/%e5%9e%8b%e6%85%8b/</link>
		<comments>http://blog.blackbing.net/2008/12/17/%e5%9e%8b%e6%85%8b/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 00:18:33 +0000</pubDate>
		<dc:creator>BLACKBING</dc:creator>
				<category><![CDATA[Javascript Core]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blackbingo.myftp.org/blog/?p=213</guid>
		<description><![CDATA[javascript是一個弱型態的語言，不像JAVA、C#等對變數型態有非常嚴謹的定義，而且一般在編譯階段就會發現型態的錯誤，讓開發者避免一些不 必要的型態錯誤。而Javascript為script語言，因此必須在執行階段才會知道錯誤，雖然目前有一些工具可以輔助你開發時幫你檢查是否有 javascript型態錯誤或者語法錯誤，例如Douglas Crockford的JSLint，就是一個非常棒的Javscript verify tool， 雖然你可以依靠這些工具輔助你 在撰寫Javascript時避免一些不必要的錯誤，但是若你打開一些Framework的原始碼，你會發現一大堆利用javascript弱型態的特性 簡化程式碼的小技巧，當然，你可以選擇不使用者些技巧，但是你卻不能不瞭解有這些特性的存在。 在Javascript中，所有的變數型態都有Boolean值，以下列出所有型態所對應的Boolean值。 Type True False String 非空字串 空字串 Number 非0 0, NaN Object Object - Array Array - undefined - undefined 既然每種型態都有Boolean值，便可以直接寫在if等判斷式裡頭，例如： var str = ''; if(!str) alert('empty'); 需要注意的是，0和空字串還有undefined都是false，因此若你的判斷式必須判斷明確的型態時，只判斷Boolean值就會出現奇怪的問題，例如： function test(idx){ if(!idx) alert('empty'); } test();//alert('empty'); test(0);//alert('empty'); test(&#34;&#34;);//alert('empty'); 在這個function test中，執行三個不同的輸入都會出現相同的結果，當你需要判斷明確的型態時，要使用「===」 或是「!==」來判斷，例如： function test2(idx){ if(typeof idx === 'undefined') alert('undefined'); [...]]]></description>
			<content:encoded><![CDATA[javascript是一個弱型態的語言，不像JAVA、C#等對變數型態有非常嚴謹的定義，而且一般在編譯階段就會發現型態的錯誤，讓開發者避免一些不 必要的型態錯誤。而Javascript為script語言，因此必須在執行階段才會知道錯誤，雖然目前有一些工具可以輔助你開發時幫你檢查是否有 javascript型態錯誤或者語法錯誤，例如Douglas Crockford的JSLint，就是一個非常棒的Javscript verify tool，
雖然你可以依靠這些工具輔助你 在撰寫Javascript時避免一些不必要的錯誤，但是若你打開一些Framework的原始碼，你會發現一大堆利用javascript弱型態的特性 簡化程式碼的小技巧，當然，你可以選擇不使用者些技巧，但是你卻不能不瞭解有這些特性的存在。
<span id="more-213"></span>在Javascript中，所有的變數型態都有Boolean值，以下列出所有型態所對應的Boolean值。
<div>
<table id="qqjo" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="25%">Type</td>
<td width="25%">True</td>
<td width="25%">False</td>
</tr>
<tr>
<td width="25%">String</td>
<td width="25%">非空字串</td>
<td width="25%">空字串</td>
</tr>
<tr>
<td width="25%">Number</td>
<td width="25%">非0</td>
<td width="25%">0, NaN</td>
</tr>
<tr>
<td valign="top">Object</td>
<td valign="top">Object</td>
<td valign="top">-</td>
</tr>
<tr>
<td valign="top">Array</td>
<td valign="top">Array</td>
<td valign="top">-</td>
</tr>
<tr>
<td valign="top">undefined</td>
<td valign="top">-</td>
<td valign="top">undefined</td>
</tr>
</tbody></table>
</div>
既然每種型態都有Boolean值，便可以直接寫在if等判斷式裡頭，例如：
<pre class="brush: jscript;">
var str = '';
if(!str)
alert('empty');
</pre>
需要注意的是，0和空字串還有undefined都是false，因此若你的判斷式必須判斷明確的型態時，只判斷Boolean值就會出現奇怪的問題，例如：
<pre class="brush: jscript;">
function test(idx){
if(!idx)
alert('empty');
}
test();//alert('empty');
test(0);//alert('empty');
test(&quot;&quot;);//alert('empty');
</pre>
在這個function test中，執行三個不同的輸入都會出現相同的結果，當你需要判斷明確的型態時，要使用「===」 或是「!==」來判斷，例如：
<pre class="brush: jscript;">
function test2(idx){
if(typeof idx === 'undefined')
alert('undefined');
else if(idx === 0)
alert('zero');
else if(idx === '')
alert('empty');
}
test();//alert('undefined');
test(0);//alert('zero');
test(&quot;&quot;);//alert('empty');
</pre>
轉 型成Boolean是一個很好用的技巧，其中最常用在對於arguments的判斷，透過對輸入值的判斷來做函式相對應的處理可以讓函式作到更彈性的運 用。例如在jQuery中，$(&#8216;.test&#8217;).html();代表取得該element的innerHTML； 而$(&#8216;.test&#8217;).html("This is my string");就代表設置該element的innerHTML。

如果你習慣了Java等語言，你可能會覺得「靠～這也太爛了吧，這樣根本就是亂寫，會發生什麼錯誤都不知道XD」，不過這是缺點也是優點，善用這個缺點， 反而變成Javascript的優點，當你寫多了Javascript之後，回去寫Java時可能又會覺得「靠～這麼嚴格～煩死了！」。 <img src="http://blog.blackbing.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=213" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.blackbing.net/2008/12/17/%e5%9e%8b%e6%85%8b/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
