<?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>三水清javascript</title>
	<atom:link href="http://js8.in/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://js8.in</link>
	<description>专注前端开发,分享Javascript/CSS/PHP等Web前端开发技巧</description>
	<lastBuildDate>Wed, 08 Feb 2012 11:59:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>javascript contains方法来判断元素包含关系</title>
		<link>http://js8.in/934.html</link>
		<comments>http://js8.in/934.html#comments</comments>
		<pubDate>Thu, 05 Jan 2012 01:49:20 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[contains]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://js8.in/?p=934</guid>
		<description><![CDATA[IE中的contains方法可以判断A元素是否包含B元素，如果包含则返回true，否则返回false，在开发中会在事件代理处理上面用到。W3C的方法是compareDocumentPosition，所以综上来说：JS通过contains和compareDocumentPosition方法来确定DOM节点间的关系,判断一个元素(对象)是否为另一个元素的子元素。
IE的contains方法很简单，但是W3C的DOMElement.contains(DOMNode)方法会返回却是一个数值。
DOMElement.contains(DOMNode)
link：https://developer.mozilla.org/En/DOM/Node.compareDocumentPosition。
DOMElement.contains(DOMNode)返回的不是一个布尔值，而是一个很奇怪的数值，它是通过如下方式累加计算出来的：





      


Bits
Number
Meaning




000000
0
元素一致


000001
1
节点在不同的文档（或者一个在文档之外）


000010
2
节点 B 在节点 A 之前


000100
4
节点 A 在节点 B 之前 


001000
8
节点 B 包含节点 A


010000
16
节点 A 包含节点 B


100000 
32
浏览器的私有使用 




综上，来个@司徒正美 大大的方案：

var contains  = function&#40;root, el&#41; &#123;
	 if &#40;root.compareDocumentPosition&#41;
		 return root === el &#124;&#124; !!&#40;root.compareDocumentPosition&#40;el&#41; &#38; 16&#41;;
	 if &#40;root.contains &#38;&#38; el.nodeType === 1&#41;&#123;
		 return root.contains&#40;el&#41; &#38;&#38; root !== el;
	 &#125;
	 while [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="通过javascript实现整站黑白效果" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F529.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/18/10979192.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">通过javascript实现整站黑白效果</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript获取光标位置以及设置光标位置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F466.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979320.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript获取光标位置以及设置光标位置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript 位运算符" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F906.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/29/11661329.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript 位运算符</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>IE中的<strong>contains</strong>方法可以判断A元素是否包含B元素，如果包含则返回true，否则返回false，在开发中会在事件代理处理上面用到。W3C的方法是<strong>compareDocumentPosition</strong>，所以综上来说：JS通过contains和compareDocumentPosition方法来确定DOM节点间的关系,判断一个元素(对象)是否为另一个元素的子元素。<br />
IE的contains方法很简单，但是W3C的<code>DOMElement.contains(DOMNode)</code>方法会返回却是一个数值。</p>
<h2>DOMElement.contains(DOMNode)</h2>
<p>link：<a target="_blank" target="_blank" href="https://developer.mozilla.org/En/DOM/Node.compareDocumentPosition">https://developer.mozilla.org/En/DOM/Node.compareDocumentPosition</a>。</p>
<p><code>DOMElement.contains(DOMNode)</code>返回的不是一个布尔值，而是一个很奇怪的数值，它是通过如下方式累加计算出来的：</p>
<table class="filament_table" cellspacing="0" width="80%" rules="cols" border="1">
<colgroup>
<col class="grey" width="20%">
<col class="yellow" width="20%">
<col class="grey">
      </colgroup>
<thead>
<tr>
<th>Bits</th>
<th>Number</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td>000000</td>
<td>0</td>
<td>元素一致</td>
</tr>
<tr>
<td>000001</td>
<td>1</td>
<td>节点在不同的文档（或者一个在文档之外）</td>
</tr>
<tr>
<td>000010</td>
<td>2</td>
<td>节点 B 在节点 A 之前</td>
</tr>
<tr>
<td>000100</td>
<td>4</td>
<td>节点 A 在节点 B 之前 </td>
</tr>
<tr>
<td>001000</td>
<td>8</td>
<td>节点 B 包含节点 A</td>
</tr>
<tr>
<td>010000</td>
<td>16</td>
<td>节点 A 包含节点 B</td>
</tr>
<tr>
<td>100000 </td>
<td>32</td>
<td>浏览器的私有使用 </td>
</tr>
</tbody>
</table>
<p><span id="more-934"></span><br />
综上，来个@司徒正美 大大的方案：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> contains  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> el<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">compareDocumentPosition</span><span style="color: #009900;">&#41;</span>
		 <span style="color: #000066; font-weight: bold;">return</span> root <span style="color: #339933;">===</span> el <span style="color: #339933;">||</span> <span style="color: #339933;">!!</span><span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">compareDocumentPosition</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">contains</span> <span style="color: #339933;">&amp;&amp;</span> el.<span style="color: #660066;">nodeType</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		 <span style="color: #000066; font-weight: bold;">return</span> root.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> root <span style="color: #339933;">!==</span> el<span style="color: #339933;">;</span>
	 <span style="color: #009900;">&#125;</span>
	 <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>el <span style="color: #339933;">=</span> el.<span style="color: #660066;">parentNode</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>el <span style="color: #339933;">===</span> root<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/934.html" title="javascript contains方法来判断元素包含关系">http://js8.in/934.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/548.html" title="通过JSONP实现完美跨域" rel="bookmark inlinks">通过JSONP实现完美跨域</a></li>
<li><a href="http://js8.in/598.html" title="Ubuntu安装NVidia驱动" rel="bookmark inlinks">Ubuntu安装NVidia驱动</a></li>
<li><a href="http://js8.in/902.html" title="IE6中javascript文件开启Gzip出现代码不执行情况" rel="bookmark inlinks">IE6中javascript文件开启Gzip出现代码不执行情况</a></li>
<li><a href="http://js8.in/703.html" title="javascript事件：获取事件对象getEvent函数" rel="bookmark inlinks">javascript事件：获取事件对象getEvent函数</a></li>
<li><a href="http://js8.in/253.html" title="腾讯发布QQ2010 Beta优先体验版本" rel="bookmark inlinks">腾讯发布QQ2010 Beta优先体验版本</a></li>
<li><a href="http://js8.in/238.html" title="真正的jQuery的radio与checkbox取值" rel="bookmark inlinks">真正的jQuery的radio与checkbox取值</a></li>
<li><a href="http://js8.in/28.html" title="08.11.16" rel="bookmark inlinks">08.11.16</a></li>
<li><a href="http://js8.in/350.html" title="javascript高效简洁代码的编写及优化技巧" rel="bookmark inlinks">javascript高效简洁代码的编写及优化技巧</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2012. |
<a href="http://js8.in/934.html#comments">One comment</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/contains" rel="tag">contains</a>, <a href="http://js8.in/tag/javascript" rel="tag">javascript</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="通过javascript实现整站黑白效果" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F529.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/18/10979192.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">通过javascript实现整站黑白效果</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript获取光标位置以及设置光标位置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F466.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979320.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript获取光标位置以及设置光标位置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript 位运算符" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F906.html&from=http%3A%2F%2Fjs8.in%2F934.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/29/11661329.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript 位运算符</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/934.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>非字母javascript</title>
		<link>http://js8.in/918.html</link>
		<comments>http://js8.in/918.html#comments</comments>
		<pubDate>Wed, 30 Nov 2011 02:17:03 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://js8.in/?p=918</guid>
		<description><![CDATA[你尝试过写javascript不用字母和数字吗？最近准备个xss的分享，记起了之前看到过老外一篇文章，内容是关于non-alpha javascript的，拿到代码分析下。
先看这段猥琐的javascript代码：

_=+&#91;&#93;;$=_++;__=&#40;_&#60;&#60;_&#41;;___=&#40;_&#60;&#60;_&#41;+_;____=__+__;_____=__+___;$$=&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_____&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#123;&#125;&#91;$&#93;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#40;$!=$&#41;+&#34;&#34;&#41;&#91;___&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;$&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;__&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_____&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;$&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;_&#93;;$$$=&#40;&#40;$!=$&#41;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#40;$!=$&#41;+&#34;&#34;&#41;&#91;__&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;___&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;$&#93;;$_$=&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_____&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#40;$!=$&#41;+&#34;&#34;&#41;&#91;__&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;__+_____&#93;+&#40;&#123;&#125;+&#34;&#34;&#41;&#91;_____&#93;+ &#40;&#123;&#125;+&#34;&#34;&#41;&#91;_&#93;+&#40;&#123;&#125;&#91;$&#93;+&#34;&#34;&#41;&#91;__&#93;+&#40;&#40;$==$&#41;+&#34;&#34;&#41;&#91;___&#93;; &#40;$&#41;&#91;$$&#93;&#91;$$&#93;&#40;$$$+&#34;('&#34;+$_$+&#34;')&#34;&#41;&#40;&#41;;

看到这里你肯定抓狂了，但是这段代码是可以运行的，运行的结果是 alert('cool code'); 。
javascript分析
这段代码能够运行，原因是javascript是弱类型的脚本语言，而且语法相对松散。例如下面的javascript是正确的，而且是有出乎意料结果的：


true+true;//2
undefined+''//'undefined'
&#40;true+&#34;&#34;&#41;&#91;0&#93;;//'t'
&#91;&#93;=='';//true
''==0;//true
typeof &#40;+''&#41;;//number,0
&#40;&#123;&#125;+''&#41;;//&#34;[object Object]&#34;
&#40;&#123;&#125;&#91;0&#93;&#41;;//undefined

所以我们想得到一个0就可以使用:

_= +&#91;&#93;;
_= +'';

下一步通过位运算，++等运算可以得到0~9所有数字。
因为0就是false，则!0就是true，({}[0]);可以得到undefined，({}+'')可以得到字符串：[object Object]，字符串中的字符，可以组成新的字符串，于是通过组合，我们就得到了："constructor"。
javascript中的点(.)运算符和[]的作用都是一样的，只不过[]可以接收传入变量、字符串，所以我们可以得到一个Function对象，即：

&#40;0&#41;&#91;'constructor'&#93;&#91;'constructor'&#93;;

另外我们也知道，下面的代码是运行的：

Function&#40;'alert(1);'&#41;&#40;&#41;;

所以，我想剩下的你就懂了……

声明：文章未声明为原创文章，本文链接 http://js8.in/918.html. 转载请注明转自 JS8.IN ™
随机文章

linux安装PHP加速器eAccelerator
windows搭建memcached环境
javascript事件：获取事件对象getEvent函数
学校DNS被人劫持啦！！！
Sina App Engine开发实例：天气预报的定时短信（二）
一道js正则笔试题
淘宝秒杀门事件始末
在普加工作的十个要求


© 三水清 for 三水清, 2011. &#124;
5 comments &#124;在微博关注我：@清-三水清
收藏到：
人人网 &#124; 豆瓣 &#124; 开心网 &#124; QQ书签 &#124; 新浪微博 &#124; 腾讯微博

Post tags: javascript
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="JavaScript跨浏览器的添加删除事件绑定函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F408.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978769.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript跨浏览器的添加删除事件绑定函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript事件触发列表与详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10982510.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript事件触发列表与详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript正则表达式详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/06/14873015.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript正则表达式详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript设置光标位置到结尾并显示出来" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F467.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/18/11027176.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript设置光标位置到结尾并显示出来</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>你尝试过写javascript不用字母和数字吗？最近准备个xss的分享，记起了之前看到过老外一篇文章，内容是关于non-alpha javascript的，拿到代码分析下。<br />
先看这段猥琐的javascript代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #339933;">=+</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>$<span style="color: #339933;">=</span>_<span style="color: #339933;">++;</span>__<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>_<span style="color: #339933;">&lt;&lt;</span>_<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>___<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>_<span style="color: #339933;">&lt;&lt;</span>_<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>_<span style="color: #339933;">;</span>____<span style="color: #339933;">=</span>__<span style="color: #339933;">+</span>__<span style="color: #339933;">;</span>_____<span style="color: #339933;">=</span>__<span style="color: #339933;">+</span>___<span style="color: #339933;">;</span>$$<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_____<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span>$<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">!=</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>___<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>$<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>__<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_____<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>$<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>$$$<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">!=</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">!=</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>__<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>___<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>$<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>$_$<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_____<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">!=</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>__<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>__<span style="color: #339933;">+</span>_____<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_____<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>_<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span>$<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>__<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">==</span>$<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>___<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>$$<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>$$<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>$$$<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;('&quot;</span><span style="color: #339933;">+</span>$_$<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>看到这里你肯定抓狂了，但是这段代码是可以运行的，运行的结果是 <code>alert('cool code');</code> 。</p>
<h2>javascript分析</h2>
<p>这段代码能够运行，原因是javascript是弱类型的脚本语言，而且语法相对松散。例如下面的javascript是正确的，而且是有出乎意料结果的：<br />
<span id="more-918"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//2</span>
undefined<span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #006600; font-style: italic;">//'undefined'</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//'t'</span>
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">''</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//true</span>
<span style="color: #3366CC;">''</span><span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//true</span>
<span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//number,0</span>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//&quot;[object Object]&quot;</span>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//undefined</span></pre></div></div>

<p>所以我们想得到一个0就可以使用:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #339933;">=</span> <span style="color: #339933;">+</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
_<span style="color: #339933;">=</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #339933;">;</span></pre></div></div>

<p>下一步通过<a href="http://js8.in/906.html" title="javascript 位运算符" target="_blank">位运算</a>，++等运算可以得到0~9所有数字。</p>
<p>因为0就是false，则!0就是true，<code>({}[0]);</code>可以得到undefined，<code>({}+'')</code>可以得到字符串：[object Object]，字符串中的字符，可以组成新的字符串，于是通过组合，我们就得到了：<code>"constructor"</code>。</p>
<p>javascript中的点(.)运算符和[]的作用都是一样的，只不过[]可以接收传入变量、字符串，所以我们可以得到一个Function对象，即：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'constructor'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'constructor'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>另外我们也知道，下面的代码是运行的：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">Function</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'alert(1);'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>所以，我想剩下的你就懂了……
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/918.html" title="非字母javascript">http://js8.in/918.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/35.html" title="评论开通了~加上啦~" rel="bookmark inlinks">评论开通了~加上啦~</a></li>
<li><a href="http://js8.in/29.html" title="08.11.13" rel="bookmark inlinks">08.11.13</a></li>
<li><a href="http://js8.in/570.html" title="十枚腾讯微博内测邀请的种子" rel="bookmark inlinks">十枚腾讯微博内测邀请的种子</a></li>
<li><a href="http://js8.in/681.html" title="WinHttp.WinHttpRequest.5.1—可以用作抓取的msxml 4.0底层对象" rel="bookmark inlinks">WinHttp.WinHttpRequest.5.1—可以用作抓取的msxml 4.0底层对象</a></li>
<li><a href="http://js8.in/447.html" title="谷歌退出中国的意图？" rel="bookmark inlinks">谷歌退出中国的意图？</a></li>
<li><a href="http://js8.in/600.html" title="jQuery教程之鼠标事件篇" rel="bookmark inlinks">jQuery教程之鼠标事件篇</a></li>
<li><a href="http://js8.in/637.html" title="【转】解析360和QQ的战争—《“听说，250干110的事儿，那就是360了”》" rel="bookmark inlinks">【转】解析360和QQ的战争—《“听说，250干110的事儿，那就是360了”》</a></li>
<li><a href="http://js8.in/314.html" title="让我们来搞搞搜狗云输入法" rel="bookmark inlinks">让我们来搞搞搜狗云输入法</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/918.html#comments">5 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="JavaScript跨浏览器的添加删除事件绑定函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F408.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978769.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript跨浏览器的添加删除事件绑定函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript事件触发列表与详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10982510.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript事件触发列表与详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript正则表达式详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/06/14873015.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript正则表达式详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript设置光标位置到结尾并显示出来" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F467.html&from=http%3A%2F%2Fjs8.in%2F918.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/18/11027176.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript设置光标位置到结尾并显示出来</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/918.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>javascript 位运算符</title>
		<link>http://js8.in/906.html</link>
		<comments>http://js8.in/906.html#comments</comments>
		<pubDate>Tue, 29 Nov 2011 02:46:09 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[位运算]]></category>

		<guid isPermaLink="false">http://js8.in/?p=906</guid>
		<description><![CDATA[前几天看到国外人在github总结的javascript的奇巧淫技（http://t.cn/herRcq），里面对位运算提到的很多，所以来科普一下javascript的位运算。于是找到了w3cschool的这篇教程，很好很强大啊，于是就忍不住转载了……
ECMAScript 整数有两种类型，即有符号整数（允许用正数和负数）和无符号整数（只允许用正数）。在 ECMAScript 中，所有整数字面量默认都是有符号整数，这意味着什么呢？
有符号整数使用 31 位表示整数的数值，用第 32 位表示整数的符号，0 表示正数，1 表示负数。数值范围从 -2147483648 到 2147483647。
可以以两种不同的方式存储二进制形式的有符号整数，一种用于存储正数，一种用于存储负数。正数是以真二进制形式存储的，前 31 位中的每一位都表示 2 的幂，从第 1 位（位 0）开始，表示 20，第 2 位（位 1）表示 21。没用到的位用 0 填充，即忽略不计。例如，下图展示的是数 18 的表示法。

18 的二进制版本只用了前 5 位，它们是这个数字的有效位。把数字转换成二进制字符串，就能看到有效位：

var iNum = 18;
alert&#40;iNum.toString&#40;2&#41;&#41;;	//输出 &#34;10010&#34;

这段代码只输出 &#8220;10010&#8243;，而不是 18 的 32 位表示。其他的数位并不重要，因为仅使用前 5 位即可确定这个十进制数值。如下图所示：

负数也存储为二进制代码，不过采用的形式是二进制补码。计算数字二进制补码的步骤有三步：

确定该数字的非负版本的二进制表示（例如，要计算 -18的二进制补码，首先要确定 18 的二进制表示）
求得二进制反码，即要把 0 替换为 1，把 1 替换为 0
在二进制反码上加 1

要确定 -18 的二进制表示，首先必须得到 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="JavaScript跨浏览器的添加删除事件绑定函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F408.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978769.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript跨浏览器的添加删除事件绑定函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript事件触发列表与详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10982510.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript事件触发列表与详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript事件：获取事件对象getEvent函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F703.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979281.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript事件：获取事件对象getEvent函数</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>前几天看到国外人在github总结的<strong>javascript</strong>的奇巧淫技（http://t.cn/herRcq），里面对位运算提到的很多，所以来科普一下javascript的位运算。于是找到了w3cschool的这篇教程，很好很强大啊，于是就忍不住转载了……</p>
<p><strong>ECMAScript</strong> 整数有两种类型，即有符号整数（允许用正数和负数）和无符号整数（只允许用正数）。在 ECMAScript 中，所有整数字面量默认都是有符号整数，这意味着什么呢？</p>
<p>有符号整数使用 31 位表示整数的数值，用第 32 位表示整数的符号，0 表示正数，1 表示负数。数值范围从 -2147483648 到 2147483647。</p>
<p>可以以两种不同的方式存储二进制形式的有符号整数，一种用于存储正数，一种用于存储负数。正数是以真二进制形式存储的，前 31 位中的每一位都表示 2 的幂，从第 1 位（位 0）开始，表示 2<sup>0</sup>，第 2 位（位 1）表示 2<sup>1</sup>。没用到的位用 0 填充，即忽略不计。例如，下图展示的是数 18 的表示法。</p>
<p><img style="border-style: initial; border-color: initial; margin-top: 25px; margin-right: 0px; margin-bottom: 0px; ;" src="http://www.w3school.com.cn/i/ct_js_integer_binary_signed_32bits.gif" alt="32 位二进制表示的有符号整数" /></p>
<p>18 的二进制版本只用了前 5 位，它们是这个数字的有效位。把数字转换成二进制字符串，就能看到有效位：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iNum <span style="color: #339933;">=</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iNum.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//输出 &quot;10010&quot;</span></pre></div></div>

<p>这段代码只输出 &#8220;10010&#8243;，而不是 18 的 32 位表示。其他的数位并不重要，因为仅使用前 5 位即可确定这个十进制数值。如下图所示：</p>
<p><img style="border-style: initial; border-color: initial; margin-top: 25px; margin-right: 0px; margin-bottom: 0px; ;" src="http://www.w3school.com.cn/i/ct_js_integer_binary_number18.gif" alt="5 位二进制表示的整数 18" /></p>
<p>负数也存储为二进制代码，不过采用的形式是二进制补码。计算数字二进制补码的步骤有三步：</p>
<ol>
<li>确定该数字的非负版本的二进制表示（例如，要计算 -18的二进制补码，首先要确定 18 的二进制表示）</li>
<li>求得二进制反码，即要把 0 替换为 1，把 1 替换为 0</li>
<li>在二进制反码上加 1</li>
</ol>
<p>要确定 -18 的二进制表示，首先必须得到 18 的二进制表示，如下所示：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">0000 0000 0000 0000 0000 0000 0001 0010</pre></div></div>

<p>接下来，计算二进制反码，如下所示：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1110</span> <span style="color: #CC0000;">1101</span></pre></div></div>

<p>最后，在二进制反码上加 1，如下所示：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1110</span> <span style="color: #CC0000;">1101</span>
                                      <span style="color: #CC0000;">1</span>
<span style="color: #339933;">---------------------------------------</span>
<span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1111</span> <span style="color: #CC0000;">1110</span> <span style="color: #CC0000;">1110</span></pre></div></div>

<p>因此，-18 的二进制表示即 1111 1111 1111 1111 1111 1111 1110 1110。记住，在处理有符号整数时，开发者不能访问 31 位。</p>
<p>有趣的是，把负整数转换成二进制字符串后，ECMAScript 并不以二进制补码的形式显示，而是用数字绝对值的标准二进制代码前面加负号的形式输出。例如：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iNum <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">18</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iNum.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//输出 &quot;-10010&quot;</span></pre></div></div>

<p>这段代码输出的是 &#8220;-10010&#8243;，而非二进制补码，这是为避免访问位 31。为了简便，ECMAScript 用一种简单的方式处理整数，使得开发者不必关心它们的用法。</p>
<p>另一方面，无符号整数把最后一位作为另一个数位处理。在这种模式中，第 32 位不表示数字的符号，而是值 2<sup>31</sup>。由于这个额外的位，无符号整数的数值范围为 0 到 4294967295。对于小于 2147483647 的整数来说，无符号整数看来与有符号整数一样，而大于 2147483647 的整数则要使用位 31（在有符号整数中，这一位总是 0）。</p>
<p>把无符号整数转换成字符串后，只返回它们的有效位。</p>
<p><span style="font-weight: bold; color: #dd0000;">注意：</span>所有整数字面量都默认存储为有符号整数。只有 ECMAScript 的位运算符才能创建无符号整数。</p>
<p><span id="more-906"></span></p>
<h2 >位运算 NOT</h2>
<p>位运算 NOT 由否定号（~）表示，它是 ECMAScript 中为数不多的与二进制算术有关的运算符之一。</p>
<p>位运算 NOT 是三步的处理过程：</p>
<ol>
<li>把运算数转换成 32 位数字</li>
<li>把二进制数转换成它的二进制反码</li>
<li>把二进制数转换成浮点数</li>
</ol>
<p>例如：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iNum1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//25 等于 00000000000000000000000000011001</span>
<span style="color: #003366; font-weight: bold;">var</span> iNum2 <span style="color: #339933;">=</span> ~iNum1<span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//转换为 11111111111111111111111111100110</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iNum2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//输出 &quot;-26&quot;</span></pre></div></div>

<p>位运算 NOT 实质上是对数字求负，然后减 1，因此 25 变 -26。用下面的方法也可以得到同样的方法：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iNum1 <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> iNum2 <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>iNum1 <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iNum2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//输出 -26</span></pre></div></div>

<h2>位运算 AND</h2>
<p>位运算 AND 由和号（&amp;）表示，直接对数字的二进制形式进行运算。它把每个数字中的数位对齐，然后用下面的规则对同一位置上的两个数位进行 AND 运算：</p>
<table style="font-family: Arial, Helvetica, sans-serif; margin-top: 10px; border-collapse: collapse;border: 1px solid #888888;" border="0">
<tbody>
<tr>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">第一个数字中的数位</th>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">第二个数字中的数位</th>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">结果</th>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
</tr>
</tbody>
</table>
<p>例如，要对数字 25 和 3 进行 AND 运算，代码如下所示：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iResult <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span> <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//输出 &quot;1&quot;</span></pre></div></div>

<p>25 和 3 进行 AND 运算的结果是 1。为什么？分析如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> <span style="color: #CC0000;">25</span> <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0001 <span style="color: #CC0000;">1001</span>
  <span style="color: #CC0000;">3</span> <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0000 0011
<span style="color: #339933;">---------------------------------------------</span>
AND <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0000 0001</pre></div></div>

<p>可以看出，在 25 和 3 中，只有一个数位（位 0）存放的都是 1，因此，其他数位生成的都是 0，所以结果为 1。</p>
<h2 >位运算 OR</h2>
<p>位运算 OR 由符号（|）表示，也是直接对数字的二进制形式进行运算。在计算每位时，OR 运算符采用下列规则：</p>
<table style="font-family: Arial, Helvetica, sans-serif; margin-top: 10px; border-collapse: collapse;border: 1px solid #888888;" border="0">
<tbody>
<tr>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">第一个数字中的数位</th>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">第二个数字中的数位</th>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">结果</th>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
</tr>
</tbody>
</table>
<p>仍然使用 AND 运算符所用的例子，对 25 和 3 进行 OR 运算，代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iResult <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span> <span style="color: #339933;">|</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//输出 &quot;27&quot;</span></pre></div></div>

<p>25 和 3 进行 OR 运算的结果是 27：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #CC0000;">25</span> <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0001 <span style="color: #CC0000;">1001</span>
 <span style="color: #CC0000;">3</span> <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0000 0011
<span style="color: #339933;">--------------------------------------------</span>
OR <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0001 <span style="color: #CC0000;">1011</span></pre></div></div>

<p>可以看出，在两个数字中，共有 4 个数位存放的是 1，这些数位被传递给结果。二进制代码 11011 等于 27。</p>
<h2 >位运算 XOR</h2>
<p>位运算 XOR 由符号（^）表示，当然，也是直接对二进制形式进行运算。XOR 不同于 OR，当只有一个数位存放的是 1 时，它才返回 1。真值表如下：</p>
<table style="font-family: Arial, Helvetica, sans-serif; margin-top: 10px; border-collapse: collapse;border: 1px solid #888888;" border="0">
<tbody>
<tr>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">第一个数字中的数位</th>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">第二个数字中的数位</th>
<th style="vertical-align: baseline; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #cccccc; text-align: left; border: 1px solid #888888;">结果</th>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">1</td>
</tr>
<tr>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
<td style="vertical-align: text-top; padding-top: 5px; padding-right: 15px; padding-bottom: 5px; padding-left: 5px; background-color: #efefef; border: 1px solid #aaaaaa;">0</td>
</tr>
</tbody>
</table>
<p>对 25 和 3 进行 XOR 运算，代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iResult <span style="color: #339933;">=</span> <span style="color: #CC0000;">25</span> <span style="color: #339933;">^</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//输出 &quot;26&quot;</span></pre></div></div>

<p>25 和 3 进行 XOR 运算的结果是 26：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> <span style="color: #CC0000;">25</span> <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0001 <span style="color: #CC0000;">1001</span>
  <span style="color: #CC0000;">3</span> <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0000 0011
<span style="color: #339933;">---------------------------------------------</span>
XOR <span style="color: #339933;">=</span> 0000 0000 0000 0000 0000 0000 0001 <span style="color: #CC0000;">1010</span></pre></div></div>

<p>可以看出，在两个数字中，共有 4 个数位存放的是 1，这些数位被传递给结果。二进制代码 11010 等于 26。</p>
<h2 >左移运算</h2>
<p>左移运算由两个小于号表示（<<）。它把数字中的所有数位向左移动指定的数量。例如，把数字 2（等于二进制中的 10）左移 5 位，结果为 64（等于二进制中的 1000000）：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iOld <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//等于二进制 10</span>
<span style="color: #003366; font-weight: bold;">var</span> iNew <span style="color: #339933;">=</span> iOld <span style="color: #339933;">&lt;&lt;</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//等于二进制 1000000 十进制 64</span></pre></div></div>

<p><span style="font-weight: bold; color: #dd0000;">注意：</span>在左移数位时，数字右边多出 5 个空位。左移运算用 0 填充这些空位，使结果成为完整的 32 位数字。</p>
<p><img style="border-style: initial; border-color: initial; margin-top: 25px; margin-right: 0px; margin-bottom: 0px; ;" src="http://www.w3school.com.cn/i/ct_js_operators_bitwise_leftshift.gif" alt="数字 2 进行左移运算" /></p>
<p><span style="font-weight: bold; color: #dd0000;">注意：</span>左移运算保留数字的符号位。例如，如果把 -2 左移 5 位，得到的是 -64，而不是 64。“符号仍然存储在第 32 位中吗？”是的，不过这在 ECMAScript 后台进行，开发者不能直接访问第 32 个数位。即使输出二进制字符串形式的负数，显示的也是负号形式（例如，-2 将显示 -10。）</p>
<h2 >有符号右移运算</h2>
<p>有符号右移运算符由两个大于号表示（&lt;$lt;）。它把 32 位数字中的所有数位整体右移，同时保留该数的符号（正号或负号）。有符号右移运算符恰好与左移运算相反。例如，把 64 右移 5 位，将变为 2：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iOld <span style="color: #339933;">=</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//等于二进制 1000000</span>
<span style="color: #003366; font-weight: bold;">var</span> iNew <span style="color: #339933;">=</span> iOld <span style="color: #339933;">&gt;&gt;</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//等于二进制 10 十进制 2</span></pre></div></div>

<p>同样，移动数位后会造成空位。这次，空位位于数字的左侧，但位于符号位之后。ECMAScript 用符号位的值填充这些空位，创建完整的数字，如下图所示：</p>
<p><img style="border-style: initial; border-color: initial; margin-top: 25px; margin-right: 0px; margin-bottom: 0px; ;" src="http://www.w3school.com.cn/i/ct_js_operators_bitwise_signedrightshift.gif" alt="数字 64 进行有符号右移运算" /></p>
<h2 >无符号右移运算</h2>
<p>无符号右移运算符由三个大于号（>>&gt;）表示，它将无符号 32 位数的所有数位整体右移。对于正数，无符号右移运算的结果与有符号右移运算一样。</p>
<p>用有符号右移运算中的例子，把 64 右移 5 位，将变为 2：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iOld <span style="color: #339933;">=</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">//等于二进制 1000000</span>
<span style="color: #003366; font-weight: bold;">var</span> iNew <span style="color: #339933;">=</span> iOld <span style="color: #339933;">&gt;&gt;&gt;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">//等于二进制 10 十进制 2</span></pre></div></div>

<p>对于负数，情况就不同了。</p>
<p>无符号右移运算用 0 填充所有空位。对于正数，这与有符号右移运算的操作一样，而负数则被作为正数来处理。</p>
<p>由于无符号右移运算的结果是一个 32 位的正数，所以负数的无符号右移运算得到的总是一个非常大的数字。例如，如果把 -64 右移 5 位，将得到 134217726。如果得到这种结果的呢？</p>
<p>要实现这一点，需要把这个数字转换成无符号的等价形式（尽管该数字本身还是有符号的），可以通过以下代码获得这种形式：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iUnsigned64 <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">64</span> <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></pre></div></div>

<p>然后，用 Number 类型的 toString() 获取它的真正的位表示，采用的基为 2：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>iUnsigned64.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这将生成 11111111111111111111111111000000，即有符号整数 -64 的二进制补码表示，不过它等于无符号整数 4294967232。</p>
<p>出于这种原因，使用无符号右移运算符要小心。</p>
<p>原文地址：http://www.w3school.com.cn/js/pro_js_operators_bitwise.asp
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/906.html" title="javascript 位运算符">http://js8.in/906.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/888.html" title="linux安装PHP加速器eAccelerator" rel="bookmark inlinks">linux安装PHP加速器eAccelerator</a></li>
<li><a href="http://js8.in/240.html" title="是不是应该出售2fool.cn的域名" rel="bookmark inlinks">是不是应该出售2fool.cn的域名</a></li>
<li><a href="http://js8.in/473.html" title="Javascript正则表达式详解" rel="bookmark inlinks">Javascript正则表达式详解</a></li>
<li><a href="http://js8.in/491.html" title="雷：蓝翔技校黑谷歌！" rel="bookmark inlinks">雷：蓝翔技校黑谷歌！</a></li>
<li><a href="http://js8.in/46.html" title="如何写网站的robots.txt和meta name robots的配置" rel="bookmark inlinks">如何写网站的robots.txt和meta name robots的配置</a></li>
<li><a href="http://js8.in/412.html" title="PHP生成RSS类：RSS.class.php" rel="bookmark inlinks">PHP生成RSS类：RSS.class.php</a></li>
<li><a href="http://js8.in/646.html" title="分享一个php验证码类，支持中文字母数字，还可自定义字体" rel="bookmark inlinks">分享一个php验证码类，支持中文字母数字，还可自定义字体</a></li>
<li><a href="http://js8.in/607.html" title="提高Firefox的innerHTML()的性能" rel="bookmark inlinks">提高Firefox的innerHTML()的性能</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/906.html#comments">One comment</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/%e4%bd%8d%e8%bf%90%e7%ae%97" rel="tag">位运算</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="JavaScript跨浏览器的添加删除事件绑定函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F408.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978769.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">JavaScript跨浏览器的添加删除事件绑定函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript事件触发列表与详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10982510.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript事件触发列表与详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript事件：获取事件对象getEvent函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F703.html&from=http%3A%2F%2Fjs8.in%2F906.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979281.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript事件：获取事件对象getEvent函数</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/906.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>IE6中javascript文件开启Gzip出现代码不执行情况</title>
		<link>http://js8.in/902.html</link>
		<comments>http://js8.in/902.html#comments</comments>
		<pubDate>Tue, 15 Nov 2011 12:25:23 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[网络技术]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://js8.in/?p=902</guid>
		<description><![CDATA[今天处理IE6下的一个bug，经过调试才发现是javascript文件文件总是执行不了，后来中午吃完饭回来才想起以前同事遇见的html中的javascript不可以执行的问题，怀疑是gzip的原因，此次的问题就是因为动态加载javascript文件导致代码不执行，经过研究发现gzip压缩过的javascript文件，如果header里Cache-Control 数据有no-cache, no-store，就会出现IE6中js代码不执行的问题。下面是转发的文章内容：
IE6下面调用开启了gzip压缩Javascript的时候，时常会碰到无法执行的情况，有可能是src调用，有可能是ajax调用，其它的浏览器工作正常，唯独IE6不能正常工作。
出现这个BUG的状况如下：

用户使用IE6浏览器
服务器没有设置 被请求文件(javascript) expires, cache-control.
服务器端开启了chunked encoding模式
通过javascrip的src调用方式或者ajax请求javascript文件
有可能是通过php生成的javascript文件
服务器端开启了gzip压缩

解决这个问题，我们要在服务器的cache-control和expires 头信息上做点工作。
经过测试，header头信息如下的gzip压缩过的javascript调用可以正常工作。
Cache-Control:maxage=1
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/x-javascript;charset=GBK
Date:Wed, 01 Dec 2010 01:24:07 GMT
Expires:Wed, 01 Dec 2010 01:24:08 GMT
Pragma:public
Server:nginx
Transfer-Encoding:chunked
分析上面的头信息，我们可以注意到：

cache-control必须要设置成maxage=time的格式，maxage是指定缓存生效时间，1为1秒
Expires必须要设置，它的时间稍大于Date即可，Date是页面请求时间，Expires是过期时间
Pragma设置为public


设置好了这两个参数，一般就可以了。
在nginx服务器端，我们需要对服务器配置：
location ~* \.(ico&#124;css&#124;js&#124;png&#124;jpeg&#124;jpg&#124;gif&#124;rar&#124;zip&#124;7z&#124;gz&#124;pdf) {
expires 1d;
}
如果javascript文件是由PHP生成的，我们就需要利用php来输出header控制了。

&#60;?php
$expires = 1;
header&#40;&#34;Pragma: public&#34;&#41;;
header&#40;&#34;Cache-Control: maxage=&#34;.$expires&#41;;
header&#40;'Expires: ' . gmdate&#40;'D, d M Y H:i:s', time&#40;&#41;+$expires&#41; . ' GMT'&#41;;
header&#40;&#34;Content-Type: application/x-javascript;charset=GBK\n&#34;&#41;;

实际应用中，需要灵活配置，Javascript文件，请尽可能通过服务器发送header头，PHP生成javascript文件，效率本来就不高，再加上额外需要输入header头，更是得不偿失。

声明：文章未声明为原创文章，本文链接 http://js8.in/902.html. 转载请注明转自 JS8.IN ™
随机文章

懂爱
一份web前端开发的简历
【转】解析360和QQ的战争—《“听说，250干110的事儿，那就是360了”》
chrome扩展应用开发教程之天气预报应用开发
园艺学院网站内测！开始，欢迎访问
前端开发常见图片格式详解
digg掘客功能上线
解决IE6不支持max-height的bug


© 三水清 for 三水清, 2011. &#124;
8 comments &#124;在微博关注我：@清-三水清
收藏到：
人人网 &#124; 豆瓣 &#124; 开心网 &#124; QQ书签 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="IE6中a标签触发图片和ajax请求被abort" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F944.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/04/14747078.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">IE6中a标签触发图片和ajax请求被abort</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="IE6下javascript不能隐藏select标签的bug" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F686.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10987937.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">IE6下javascript不能隐藏select标签的bug</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="解决IE6不支持max-height的bug" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F606.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978851.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">解决IE6不支持max-height的bug</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="IE6 PNG图片不透明问题的解决方法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F380.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/01/2770440.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">IE6 PNG图片不透明问题的解决方法</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>今天处理IE6下的一个bug，经过调试才发现是<strong>javascript</strong>文件文件总是执行不了，后来中午吃完饭回来才想起以前同事遇见的html中的javascript不可以执行的问题，怀疑是gzip的原因，此次的问题就是因为动态加载javascript文件导致代码不执行，经过研究发现gzip压缩过的javascript文件，如果header里Cache-Control 数据有no-cache, no-store，就会出现IE6中js代码不执行的问题。下面是转发的文章内容：</p>
<p>IE6下面调用开启了gzip压缩Javascript的时候，时常会碰到无法执行的情况，有可能是src调用，有可能是ajax调用，其它的浏览器工作正常，唯独IE6不能正常工作。<br />
出现这个BUG的状况如下：</p>
<ol>
<li>用户使用IE6浏览器</li>
<li>服务器没有设置 被请求文件(javascript) expires, cache-control.</li>
<li>服务器端开启了chunked encoding模式</li>
<li>通过javascrip的src调用方式或者ajax请求javascript文件</li>
<li>有可能是通过php生成的javascript文件</li>
<li>服务器端开启了gzip压缩</li>
</ol>
<p>解决这个问题，我们要在服务器的cache-control和expires 头信息上做点工作。<br />
经过测试，header头信息如下的gzip压缩过的javascript调用可以正常工作。</p>
<blockquote><p>Cache-Control:maxage=1<br />
Connection:keep-alive<br />
Content-Encoding:gzip<br />
Content-Type:application/x-javascript;charset=GBK<br />
Date:Wed, 01 Dec 2010 01:24:07 GMT<br />
Expires:Wed, 01 Dec 2010 01:24:08 GMT<br />
Pragma:public<br />
Server:nginx<br />
Transfer-Encoding:chunked</p></blockquote>
<p>分析上面的头信息，我们可以注意到：</p>
<ol>
<li>cache-control必须要设置成maxage=time的格式，maxage是指定缓存生效时间，1为1秒</li>
<li>Expires必须要设置，它的时间稍大于Date即可，Date是页面请求时间，Expires是过期时间</li>
<li>Pragma设置为public</li>
</ol>
<p><span id="more-902"></span><br />
设置好了这两个参数，一般就可以了。</p>
<p>在nginx服务器端，我们需要对服务器配置：</p>
<blockquote><p>location ~* \.(ico|css|js|png|jpeg|jpg|gif|rar|zip|7z|gz|pdf) {<br />
expires 1d;<br />
}</p></blockquote>
<p>如果javascript文件是由PHP生成的，我们就需要利用php来输出header控制了。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$expires</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pragma: public&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: maxage=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$expires</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Expires: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'D, d M Y H:i:s'</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #000088;">$expires</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' GMT'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: application/x-javascript;charset=GBK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>实际应用中，需要灵活配置，Javascript文件，请尽可能通过服务器发送header头，PHP生成javascript文件，效率本来就不高，再加上额外需要输入header头，更是得不偿失。
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/902.html" title="IE6中javascript文件开启Gzip出现代码不执行情况">http://js8.in/902.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/442.html" title="获取滔滔头像图片地址的算法" rel="bookmark inlinks">获取滔滔头像图片地址的算法</a></li>
<li><a href="http://js8.in/519.html" title="windows下安装基于Apache的SVN服务器" rel="bookmark inlinks">windows下安装基于Apache的SVN服务器</a></li>
<li><a href="http://js8.in/67.html" title="校内人人网忽悠用户-陈一舟牛啊~" rel="bookmark inlinks">校内人人网忽悠用户-陈一舟牛啊~</a></li>
<li><a href="http://js8.in/472.html" title="Javascript在IE与Firefox下的差异写法总结" rel="bookmark inlinks">Javascript在IE与Firefox下的差异写法总结</a></li>
<li><a href="http://js8.in/630.html" title="我开发的新浪微博应用：微博送祝福" rel="bookmark inlinks">我开发的新浪微博应用：微博送祝福</a></li>
<li><a href="http://js8.in/17.html" title="windows Xp和office盗版软件避免十月20号之后出现黑屏方法" rel="bookmark inlinks">windows Xp和office盗版软件避免十月20号之后出现黑屏方法</a></li>
<li><a href="http://js8.in/440.html" title="谷歌500元免费AdWords广告费申请第二步" rel="bookmark inlinks">谷歌500元免费AdWords广告费申请第二步</a></li>
<li><a href="http://js8.in/492.html" title="十大Google Buzz的使用技巧" rel="bookmark inlinks">十大Google Buzz的使用技巧</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/902.html#comments">8 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/bug" rel="tag">bug</a>, <a href="http://js8.in/tag/ie6" rel="tag">IE6</a>, <a href="http://js8.in/tag/javascript" rel="tag">javascript</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="IE6中a标签触发图片和ajax请求被abort" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F944.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/04/14747078.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">IE6中a标签触发图片和ajax请求被abort</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="IE6下javascript不能隐藏select标签的bug" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F686.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10987937.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">IE6下javascript不能隐藏select标签的bug</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="解决IE6不支持max-height的bug" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F606.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978851.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">解决IE6不支持max-height的bug</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="IE6 PNG图片不透明问题的解决方法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F380.html&from=http%3A%2F%2Fjs8.in%2F902.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/01/2770440.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">IE6 PNG图片不透明问题的解决方法</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/902.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>javascript的String.replace的妙用</title>
		<link>http://js8.in/894.html</link>
		<comments>http://js8.in/894.html#comments</comments>
		<pubDate>Tue, 08 Nov 2011 11:33:54 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[代码优化]]></category>

		<guid isPermaLink="false">http://js8.in/?p=894</guid>
		<description><![CDATA[作为前端开发人员，总会有点自己的奇技淫巧，毕竟前端开发不同于后端，代码全部暴漏给用户不说，代码冗余了少则影响带宽，多则效率降低。所以今天我也来爆个自己的奇技淫巧。
javascript的String.replace方法应该javascript前端开发工程师都了解，可是你掌握了吗？replace有接受两个参数，第一个参数可以是字符串，也可以是正则表达式，第二个参数除支持字符串之外，还支持$1形式正则匹配的文本，除此之外还支持传入一个处理函数，这个函数的return值就是要替换成的内容。
了解更多javascript的String.replace用法，访问：http://www.w3school.com.cn/js/jsref_replace.asp
String.replace的妙用
在《javascript获取隐藏dom的宽高》中，我就使用了replace，使用replace做一些重复性的操作，减少不必要的代码。
其实这里说的重复性的工作就是循环！例如，我们要获取假如我要求id为div1和div2的两个元素的宽高，一部分人会一点一点的做，先算完div1的，然后做div2的，这样你是否觉得有部分是充分的操作呢，于是可以将取宽高的函数独立出来，这样多了一个函数，而且函数只用这两次，似乎有点浪费。
于是一部分人想到了使用split切分字符串，然后用Array.forEach做循环。例如下面的代码：

&#34;div1 div2&#34;.split&#40;' '&#41;.forEach&#40;function&#40;a,i&#41;&#123;
    console.log&#40;a&#41;;
	//do something
&#125;&#41;


这样对于支持forEach的高级浏览器还是可以的，否则需要扩展数组原型了，不提倡扩展原型，即使不扩展原型提供个额外的函数来操作，就又是浪费，
所以我的做法是使用replace，代码如下：

var wh = &#123;&#125;;
&#34;div1 div2&#34;.replace&#40;/[^ ]+/g,function&#40;a&#41;&#123;
	var elem = document.getElementById&#40;a&#41;;
        wh&#91;a&#93; = &#123;&#125;;
	'Width Height'.replace&#40;/[^ ]+/g, function&#40;i&#41;&#123;
			wh&#91;a&#93;&#91;i&#93; = elem&#91;'offset' + i&#93; &#124;&#124; elem&#91;'client' + i&#93;;
		&#125;&#41;;
&#125;&#41;;
console.log&#40;wh&#41;;

有人说不好懂，而且有点太……，对就是有点装B，呵呵~但是简洁，前端开发的代码还是简洁点好。另外还有通过位操作来判断String.indexOf返回的是否为-1，也是个不错的方法~呵呵，不多说了

声明：文章未声明为原创文章，本文链接 http://js8.in/894.html. 转载请注明转自 JS8.IN ™
随机文章

是不是应该出售2fool.cn的域名
08.11.18
新浪微博jsSDK操作指南
chrome扩展应用开发教程之开发chrome应用基础
chrome字体最小值12px问题及解决方法
最新版WINDOWS 7 SP1下载地址出现
IE6中javascript文件开启Gzip出现代码不执行情况
迅雷7(Thunder7) 最新版下载地址&#8211;绿化安装去广告


© 三水清 for 三水清, 2011. &#124;
2 comments &#124;在微博关注我：@清-三水清
收藏到：
人人网 &#124; 豆瓣 &#124; 开心网 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript高效简洁代码的编写及优化技巧" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F350.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/18/11026179.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript高效简洁代码的编写及优化技巧</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="如何写出高质量的Javascript代码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F676.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979856.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">如何写出高质量的Javascript代码</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript获取光标位置以及设置光标位置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F466.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979320.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript获取光标位置以及设置光标位置</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>作为<strong>前端开发</strong>人员，总会有点自己的奇技淫巧，毕竟前端开发不同于后端，代码全部暴漏给用户不说，代码冗余了少则影响带宽，多则效率降低。所以今天我也来爆个自己的奇技淫巧。</p>
<p>j<strong>avascript</strong>的String.replace方法应该javascript前端开发工程师都了解，可是你掌握了吗？replace有接受两个参数，第一个参数可以是字符串，也可以是正则表达式，第二个参数除支持字符串之外，还支持<code>$1</code>形式正则匹配的文本，除此之外还支持传入一个处理函数，这个函数的<code>return</code>值就是要替换成的内容。<br />
了解更多javascript的String.replace用法，访问：http://www.w3school.com.cn/js/jsref_replace.asp</p>
<h3>String.replace的妙用</h3>
<p>在《<a href="http://js8.in/781.html" title="javascript获取隐藏dom的宽高" target="_blank">javascript获取隐藏dom的宽高</a>》中，我就使用了replace，使用replace做一些重复性的操作，减少不必要的代码。</p>
<p>其实这里说的重复性的工作就是循环！例如，我们要获取假如我要求id为div1和div2的两个元素的宽高，一部分人会一点一点的做，先算完div1的，然后做div2的，这样你是否觉得有部分是充分的操作呢，于是可以将取宽高的函数独立出来，这样多了一个函数，而且函数只用这两次，似乎有点浪费。</p>
<p>于是一部分人想到了使用split切分字符串，然后用Array.forEach做循环。例如下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;div1 div2&quot;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">//do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><span id="more-894"></span><br />
这样对于支持forEach的高级浏览器还是可以的，否则需要扩展数组原型了，不提倡扩展原型，即使不扩展原型提供个额外的函数来操作，就又是浪费，</p>
<p>所以我的做法是使用replace，代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> wh <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #3366CC;">&quot;div1 div2&quot;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^ ]+/g</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        wh<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #3366CC;">'Width Height'</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^ ]+/g</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			wh<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> elem<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'offset'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> elem<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'client'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>wh<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>有人说不好懂，而且有点太……，对就是有点装B，呵呵~但是简洁，<strong>前端开发</strong>的代码还是简洁点好。另外还有通过位操作来判断String.indexOf返回的是否为-1，也是个不错的方法~呵呵，不多说了</p>
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/894.html" title="javascript的String.replace的妙用">http://js8.in/894.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/534.html" title="官方中文正式原版PhotoShop CS5下载" rel="bookmark inlinks">官方中文正式原版PhotoShop CS5下载</a></li>
<li><a href="http://js8.in/14.html" title="王老师突然问：考研吗？" rel="bookmark inlinks">王老师突然问：考研吗？</a></li>
<li><a href="http://js8.in/9.html" title="生日party" rel="bookmark inlinks">生日party</a></li>
<li><a href="http://js8.in/682.html" title="最短的DOMReady代码" rel="bookmark inlinks">最短的DOMReady代码</a></li>
<li><a href="http://js8.in/408.html" title="JavaScript跨浏览器的添加删除事件绑定函数" rel="bookmark inlinks">JavaScript跨浏览器的添加删除事件绑定函数</a></li>
<li><a href="http://js8.in/792.html" title="基于express+socket.io的nodejs聊天室" rel="bookmark inlinks">基于express+socket.io的nodejs聊天室</a></li>
<li><a href="http://js8.in/539.html" title="jQuery弹出框插件wBox 1.0正式发布" rel="bookmark inlinks">jQuery弹出框插件wBox 1.0正式发布</a></li>
<li><a href="http://js8.in/460.html" title="做个中国工会网竟然670万！" rel="bookmark inlinks">做个中国工会网竟然670万！</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/894.html#comments">2 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/%e4%bb%a3%e7%a0%81%e4%bc%98%e5%8c%96" rel="tag">代码优化</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript高效简洁代码的编写及优化技巧" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F350.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/18/11026179.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript高效简洁代码的编写及优化技巧</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="如何写出高质量的Javascript代码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F676.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979856.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">如何写出高质量的Javascript代码</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript获取光标位置以及设置光标位置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F466.html&from=http%3A%2F%2Fjs8.in%2F894.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979320.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript获取光标位置以及设置光标位置</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/894.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>javascript作用域和作用域链</title>
		<link>http://js8.in/875.html</link>
		<comments>http://js8.in/875.html#comments</comments>
		<pubDate>Wed, 26 Oct 2011 03:26:22 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[作用域]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=875</guid>
		<description><![CDATA[javascript的作用域是一个重要的知识点，javascript作用域（scope）是通过javascript的作用域链（scope chain）来实现的。
javascript作用域
javascript作用域(scope)：简单的说，就是创建一个函数时在什么环境下创建的，它控制了javascript代码运行时变量和函数的访问范围。在JavaScript中，变量的作用域有全局作用域和局部作用域两种。
全局作用域（Global Scope）
在代码中任何地方都能访问到的对象拥有全局作用域，注意：全局变量是魔鬼！因为它效率低（后面讲到），污染全局环境！一般有一下三种方式获取全局作用域。
代码最外层定义的函数和变量拥有全局作用域
在代码的最外层，定义的函数、变量，都是拥有全局作用域的。

var a = 1;
function b&#40;&#41;&#123;
    var a = 2;
&#125;
b&#40;&#41;;
alert&#40;a&#41;;//1

函数内部不使用var定义的拥有全局作用域
在函数内部，不使用var定义的变量拥有全局作用域，这是个坑！要注意，很多前端开发工程师不习惯写var，其实这时候你已经污染了全局作用域！

var a = 1;
function b&#40;&#41;&#123;
    a = 2;
    c = 3;
&#125;
b&#40;&#41;;
alert&#40;a&#41;;//2
alert&#40;c&#41;;//3


所有window对象的属性拥有全局作用域
在《正确理解javascript的this关键字》中我提到了脚本语言的运行需要宿主，在浏览器的全局对象是window，所以全局的变量和函数是window的属性，并且拥有全局作用域。

function a&#40;&#41;&#123;
	window.b = 1;
&#125;
a&#40;&#41;;
alert&#40;b&#41;;//1

局部作用域（Local Scope）
和全局作用域相反，局部作用域一般只在固定的代码片段内可访问到，最常见的例如函数内部，所有在一些地方也会看到有人把这种作用域称为函数作用域。如下面的代码中b就是一个局部变量，在函数外部是访问不到的。

var a = function&#40;&#41;&#123;
	var b = 1;
	alert&#40;b&#41;;
&#125;
a&#40;&#41;;
alert&#40;b&#41;;//出错 b undefined

javascript作用域链
javascript作用域（scope）是通过javascript的作用域链（scope chain）来实现的。javascript函数对象中拥有一个仅供javascript引擎访问的内部属性——[[Scope]]，[[Scope]]指向一个集合，即为“作用域链（Scope chain）”，它决定了哪些数据能被函数访问。

var a = 1;
function fn1&#40;&#41;&#123;
    var a = 9;
 [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript的词法作用域" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F745.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/10/23/9665591.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript的词法作用域</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="正确理解javascript的this关键字" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F851.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978049.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">正确理解javascript的this关键字</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript正则表达式详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/06/14873015.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript正则表达式详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>javascript的<strong>作用域</strong>是一个重要的知识点，javascript作用域（scope）是通过javascript的作用域链（scope chain）来实现的。</p>
<h2>javascript作用域</h2>
<p>javascript作用域(scope)：简单的说，就是创建一个函数时在什么环境下创建的，它控制了javascript代码运行时变量和函数的访问范围。在JavaScript中，变量的作用域有全局作用域和局部作用域两种。</p>
<h3>全局作用域（Global Scope）</h3>
<p>在代码中任何地方都能访问到的对象拥有全局作用域，注意：全局变量是魔鬼！因为它效率低（后面讲到），污染全局环境！一般有一下三种方式获取全局作用域。</p>
<h4>代码最外层定义的函数和变量拥有全局作用域</h4>
<p>在代码的最外层，定义的函数、变量，都是拥有全局作用域的。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//1</span></pre></div></div>

<h4>函数内部不使用var定义的拥有全局作用域</h4>
<p>在函数内部，不使用var定义的变量拥有全局作用域，这是个坑！要注意，很多前端开发工程师不习惯写var，其实这时候你已经污染了全局作用域！</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    a <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
    c <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//2</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//3</span></pre></div></div>

<p><span id="more-875"></span></p>
<h4>所有window对象的属性拥有全局作用域</h4>
<p>在《<a href="http://js8.in/851.html" target="_blank" title="javascript的this关键字">正确理解javascript的this关键字</a>》中我提到了脚本语言的运行需要宿主，在浏览器的全局对象是window，所以全局的变量和函数是window的属性，并且拥有<strong>全局作用域</strong>。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> a<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	window.<span style="color: #660066;">b</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
a<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//1</span></pre></div></div>

<h3>局部作用域（Local Scope）</h3>
<p>和全局作用域相反，局部作用域一般只在固定的代码片段内可访问到，最常见的例如函数内部，所有在一些地方也会看到有人把这种作用域称为函数作用域。如下面的代码中b就是一个局部变量，在函数外部是访问不到的。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
a<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//出错 b undefined</span></pre></div></div>

<h2>javascript作用域链</h2>
<p><strong>javascript作用域</strong>（scope）是通过javascript的作用域链（scope chain）来实现的。javascript函数对象中拥有一个仅供javascript引擎访问的内部属性——[[Scope]]，[[Scope]]指向一个集合，即为“作用域链（Scope chain）”，它决定了哪些数据能被函数访问。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> fn1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">function</span> fn2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//1</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//9</span>
    <span style="color: #009900;">&#125;</span>
    fn2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
fn1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>上面代码的<strong>作用域链</strong>，可以用下面的图来表示：</p>
<div id="attachment_876" class="wp-caption aligncenter" style="width: 310px"><a href="http://js8.in/875.html/10-25_155941" rel="attachment wp-att-876"><img src="http://js8.in/wp-content/uploads/2011/10/10-25_155941-300x211.png" alt="javascript的作用域链图示" title="javascript的作用域链图示" width="300" height="211" class="size-medium wp-image-876" /></a><p class="wp-caption-text">javascript的作用域链图示</p></div>
<h3>作用域链与javascript代码优化</h3>
<p>代码在运行时，变量的查找总是从作用域链的底部开始往上查找，如果第一层没找到，就要从更高一级作用域查找，这样一直找下去，一直找到全局作用域，如果没有找到则返回undefined。</p>
<p>从作用域链的结构可以看出，在运行期上下文的作用域链中，标识符所在的位置越深，读写速度就会越慢。因为全局变量总是存在于运行期上下文作用域链的最末端，因此在标识符解析的时候，查找全局变量是最慢的。所以，在编写代码的时候应尽量少使用全局变量，尽可能使用局部变量。</p>
<p>一个好的经验法则是：如果一个跨作用域的对象被引用了一次以上，则先把它存储到局部变量里再使用。如下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> changeColor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;btnChange&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;targetCanvas&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这个函数引用了两次全局变量document，查找该变量必须遍历整个作用域链，直到最后在全局对象中才能找到。这段代码可以重写如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> changeColor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> doc<span style="color: #339933;">=</span>document<span style="color: #339933;">;</span>
    doc.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;btnChange&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        doc.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;targetCanvas&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这段代码比较简单，重写后不会显示出巨大的性能提升，但是如果程序中有大量的全局变量被从反复访问，那么重写后的代码性能会有显著改善。</p>
<h3>作用域链的延长</h3>
<p>当执行流进入下列任何一个语句时，<strong>作用域链</strong>将得到延长：</p>
<blockquote><ol>
<li>try-catch语句的catch块</li>
<li>with语句</li>
</ol>
</blockquote>
<p>此两个语句会在作用域链的前端添加一个变量对象。对with来说，其变量对象中包含着指定对象的所有属性和方法所作的变量申明；对catch来说，其变量对象中包含的是被抛出的错误对象的申明。这些标量对象都是只读的，因此在with和catch语句中申明的变量都会被添加到所在执行环境的变量对象中。</p>
<p>当with和catch语句结束之后，作用域链会恢复到原先的状态。</p>
<p>值得一提的是改变了javascript的作用域链之后，代码效率会降低。拿with语句来说，当代码运行到with语句时，运行期上下文的作用域链临时被改变了。一个新的可变对象被创建，它包含了参数指定的对象的所有属性。这个对象将被推入作用域链的头部，这意味着函数的所有局部变量现在处于第二个作用域链对象中，因此访问代价更高了。</p>
<p>继续阅读关于javascript的作用域的文章：</p>
<blockquote><ol>
<li><a href="http://js8.in/745.html" title="javascript的词法作用域" target="_blank">javascript的词法作用域</a></li>
<li><a href="http://js8.in/851.html" title="" target="_blank">正确理解javascript的this关键字</a></li>
</ol>
</blockquote>
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/875.html" title="javascript作用域和作用域链">http://js8.in/875.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/580.html" title="爱墙（许愿墙）程序后台添加置顶、搜索等功能" rel="bookmark inlinks">爱墙（许愿墙）程序后台添加置顶、搜索等功能</a></li>
<li><a href="http://js8.in/271.html" title="重装系统啦~看看桌面截图吧~" rel="bookmark inlinks">重装系统啦~看看桌面截图吧~</a></li>
<li><a href="http://js8.in/680.html" title="一份web前端开发的简历" rel="bookmark inlinks">一份web前端开发的简历</a></li>
<li><a href="http://js8.in/289.html" title="用批处理说中文！" rel="bookmark inlinks">用批处理说中文！</a></li>
<li><a href="http://js8.in/547.html" title="去掉Eclipse插件Aptana启动显示My Aptana" rel="bookmark inlinks">去掉Eclipse插件Aptana启动显示My Aptana</a></li>
<li><a href="http://js8.in/894.html" title="javascript的String.replace的妙用" rel="bookmark inlinks">javascript的String.replace的妙用</a></li>
<li><a href="http://js8.in/531.html" title="jQuery插件wBox准备更新" rel="bookmark inlinks">jQuery插件wBox准备更新</a></li>
<li><a href="http://js8.in/373.html" title="推荐六个在线制作Loading图标的网站" rel="bookmark inlinks">推荐六个在线制作Loading图标的网站</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/875.html#comments">2 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/%e4%bd%9c%e7%94%a8%e5%9f%9f" rel="tag">作用域</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript的词法作用域" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F745.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/10/23/9665591.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript的词法作用域</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="正确理解javascript的this关键字" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F851.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978049.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">正确理解javascript的this关键字</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript正则表达式详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/06/14873015.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript正则表达式详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F875.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/875.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>正确理解javascript的this关键字</title>
		<link>http://js8.in/851.html</link>
		<comments>http://js8.in/851.html#comments</comments>
		<pubDate>Tue, 25 Oct 2011 00:55:30 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[作用域]]></category>
		<category><![CDATA[面向对象]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=851</guid>
		<description><![CDATA[javascript有this关键字，this跟javascript的执行上下文密切相关，很多前端开发工程师至今对this关键字还是模棱两可，本文将结合代码讲解下javascript的this关键字。
this和对象的关系
首先来看下面的代码：

var person = &#123;
	name:'Theo Wong',
	gender:'male',
	getName:function&#40;&#41;&#123;
		console.log&#40;person.name&#41;;
	&#125;
&#125;;
person.getName&#40;&#41;;

定义了一个person对象，对象中包含了name、gender属性，还包括了一个getName的方法，其作用是输出person对象的name。在这种情况下，我们可以使用this来在person对象中代替person对象本身，所以上面的代码跟下面的直接结果是一样的：

var person = &#123;
	name:'Theo Wong',
	gender:'male',
	getName:function&#40;&#41;&#123;
		console.log&#40;this.name&#41;;
	&#125;
&#125;;
person.getName&#40;&#41;;

请记住一点：this永远指向的是函数对象的所有者！上面的例子中getName的所有者是person对象，所以this指代的是person。
this和全局对象
我们再来看看再全局对象中，this指代的是什么，我们知道javascript是脚本语言，所以javascript的执行需要有一个宿主环境，在浏览器中这个宿主环境就是window对象，所以在全局函数中，this指代的是window对象（除非使用new，call，apply方法来改变this的指代关系）。懂得了这个关键点，下面的代码就好理解了：

var a = 1;
console.log&#40;a&#41;;//1
console.log&#40;this.a&#41;;//1
console.log&#40;window.a&#41;;//1

很多前端开发工程师经常使用在函数名字之前添加个window来调用函数，这是因为在浏览器中全局对象就是window，所有的函数变量都是在window对象之中，所以下面的代码中的this指代window对象就好理解了：

var a = 1;
function foo&#40;&#41;&#123;
	var b = 2;
	console.log&#40;this.a+b&#41;;//3
&#125;
foo&#40;&#41;;

所以说，只要记住：this永远指向的是函数对象的所有者，即this的值是由激活执行上下文代码的调用者决定的，就好理解this的指代关系了。

函数构造器中的this
当函数作为构造器使用new关键字实例化时，this的指代关系又是怎样的呢？看下面的代码：

var Person = function&#40;&#41;&#123;
	this.name = 'Theo Wong';
&#125;
var person = new Person&#40;&#41;;
console.log&#40;person.name&#41;;

new执行过程会首先执行Person的构造器[[construct]]，然后在调用[[call]]方法给this赋值，这个执行过程可以简单理解为三步

首先建立一个空的对象object，类似var obj={}
然后将空对象使用Person的call操作，类似Person.call(obj)
执行完Person之后再return this，完成new过程，赋值给person变量

所以经过new加工过的函数，this的函数调用者是Person本身，而不是window了。
嵌套函数中的this
在嵌套函数中，this的指代关系有会是怎样的呢？看下面的代码：

var myObject = &#123;
  func1:function&#40;&#41; &#123;
     console.log&#40;this&#41;; //myObject
     var func2=function&#40;&#41; &#123;
      [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript作用域和作用域链" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F875.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10981959.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript作用域和作用域链</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript的词法作用域" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F745.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/10/23/9665591.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript的词法作用域</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript继承的写法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F698.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/02/20576222.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript继承的写法</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>javascript有this关键字，this跟<strong>javascript</strong>的执行上下文密切相关，很多前端开发工程师至今对this关键字还是模棱两可，本文将结合代码讲解下javascript的<strong>this关键字</strong>。</p>
<h2>this和对象的关系</h2>
<p>首先来看下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Theo Wong'</span><span style="color: #339933;">,</span>
	gender<span style="color: #339933;">:</span><span style="color: #3366CC;">'male'</span><span style="color: #339933;">,</span>
	getName<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>person.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
person.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>定义了一个person对象，对象中包含了name、gender属性，还包括了一个getName的方法，其作用是输出person对象的name。在这种情况下，我们可以使用this来在person对象中代替person对象本身，所以上面的代码跟下面的直接结果是一样的：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Theo Wong'</span><span style="color: #339933;">,</span>
	gender<span style="color: #339933;">:</span><span style="color: #3366CC;">'male'</span><span style="color: #339933;">,</span>
	getName<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
person.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>请记住一点：<strong>this永远指向的是函数对象的所有者</strong>！上面的例子中getName的所有者是person对象，所以this指代的是person。</p>
<h2>this和全局对象</h2>
<p>我们再来看看再全局对象中，this指代的是什么，我们知道javascript是脚本语言，所以javascript的执行需要有一个宿主环境，在浏览器中这个宿主环境就是window对象，所以在全局函数中，this指代的是window对象（除非使用new，call，apply方法来改变this的指代关系）。懂得了这个关键点，下面的代码就好理解了：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//1</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//1</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//1</span></pre></div></div>

<p>很多<strong>前端开发</strong>工程师经常使用在函数名字之前添加个window来调用函数，这是因为在浏览器中全局对象就是window，所有的函数变量都是在window对象之中，所以下面的代码中的this指代window对象就好理解了：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span><span style="color: #339933;">+</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//3</span>
<span style="color: #009900;">&#125;</span>
foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>所以说，只要记住：<strong>this</strong>永远指向的是函数对象的所有者，即this的值是由激活执行上下文代码的调用者决定的，就好理解this的指代关系了。<br />
<span id="more-851"></span></p>
<h2>函数构造器中的this</h2>
<p>当函数作为构造器使用new关键字实例化时，this的指代关系又是怎样的呢？看下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Theo Wong'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>person.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>new执行过程会首先执行Person的构造器[[construct]]，然后在调用[[call]]方法给this赋值，这个执行过程可以简单理解为三步</p>
<ol>
<li>首先建立一个空的对象object，类似var obj={}</li>
<li>然后将空对象使用Person的call操作，类似Person.call(obj)</li>
<li>执行完Person之后再return this，完成new过程，赋值给person变量</li>
</ol>
<p>所以经过new加工过的函数，this的函数调用者是Person本身，而不是window了。</p>
<h2>嵌套函数中的this</h2>
<p>在嵌套函数中，this的指代关系有会是怎样的呢？看下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  func1<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//myObject</span>
     <span style="color: #003366; font-weight: bold;">var</span> func2<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//window</span>
        <span style="color: #003366; font-weight: bold;">var</span> func3<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//window</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
myObject.<span style="color: #660066;">func1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>在嵌套函数中，由于嵌套函数的执行上下文是window，所以this指代的是window对象，其实这是ECMA-262-3的一个bug，在最新的ECMA-262-5中已经修复。</p>
<h2>事件处理中的this</h2>
<p>在javascript中处理事件函数中，this的指代关系就更加扑朔离迷了。我们建立一个showValue函数，函数内容如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> showValue <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>现在有个input，我们给input元素添加click事件，当点击input时触发showValue函数，看看现在的this指代的是什么对象。</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;test&quot; type=&quot;text&quot; /&gt;</pre></div></div>

<h3>通过dom.onclick绑定事件</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> showValue<span style="color: #339933;">;</span></pre></div></div>

<p>运行代码会得到预期的结果，showValue虽然定义在全局对象中，但是当采用了onclick的绑定方式时，showValue是作为dom的onclick方法被调用的，所以它的this应该指代的是dom对象，而不再是<strong>window</strong>对象。</p>
<h3>写在html标签内</h3>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;test&quot; type=&quot;text&quot; onclick=&quot;showValue();&quot; /&gt;</pre></div></div>

<p>当点击dom时，我们获取不到正确的this，这是为什么呢？</p>
<p>此时的this指代的是window对象，因为window对象中没有定义value的值，所以获取不到<code>this.value</code>。其实此时的不是将showValue函数赋值给dom对象的onclick，而是引用！所以上面的代码跟下面的代码关系是一样的：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    showValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>根据上面说的<strong>javascript</strong>嵌套函数的this值，我们可以得出现在showValue的this其实是window。</p>
<h3>通过addEventListener/attachEvent绑定事件监听</h3>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;text&quot; id=&quot;test&quot; /&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var dom = document.getElementById('test');
id = 'window';
function test(){
	alert(this.id);
}
dom.addEventListener?dom.addEventListener('click',test,false):dom.attachEvent('onclick',test);
//addEventListener test
//attachEvent window
&lt;/script&gt;</pre></div></div>

<p>这种绑定事件监听的方式，attachEvent this是window对象，而addEventListener则是dom对象的。@魔堕轮回 提出来的bug~嘎嘎</p>
<h2>使用call和apply方法改变this</h2>
<p>在Function对象原型（Function.prototype）中有两个方法：call和apply，通过call和apply可以改变this的值， 它们都接受第一个参数作为调用执行上下文中this的值。它们的不同点就是apply第二个参数为数组，call接收的参数是依次传入的。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'Theo Wong'</span><span style="color: #339933;">,</span>
	desc<span style="color: #339933;">:</span><span style="color: #3366CC;">'一个前端开发者'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> getInfo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">desc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
getInfo.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//Theo Wong一个前端开发者</span></pre></div></div>

<h2>总结</h2>
<p>this是javascript的重要关键字，理解掌握this关键字在不同的执行上下文指代关系，才能避免代码犯一些不必要的错误。深入了解javascript的代码执行过程，及其执行上下文，推荐阅读《<a href="http://js8.in/745.html" title="javascript执行上下文词法作用域">javascript的词法作用域</a>》
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/851.html" title="正确理解javascript的this关键字">http://js8.in/851.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/293.html" title="【续】教你偷老师的课件甚至考试试题~" rel="bookmark inlinks">【续】教你偷老师的课件甚至考试试题~</a></li>
<li><a href="http://js8.in/555.html" title="Img,Script,Link等标签为空可导致页面多余请求" rel="bookmark inlinks">Img,Script,Link等标签为空可导致页面多余请求</a></li>
<li><a href="http://js8.in/574.html" title="只要20元，你也可以做个QQ爱墙" rel="bookmark inlinks">只要20元，你也可以做个QQ爱墙</a></li>
<li><a href="http://js8.in/520.html" title="使用Synergy实现多台电脑共享一套鼠标键盘" rel="bookmark inlinks">使用Synergy实现多台电脑共享一套鼠标键盘</a></li>
<li><a href="http://js8.in/510.html" title="提供一个免费支持Jabber, AIM, MSN, Yahoo, Gtalk,Twitter 机器人" rel="bookmark inlinks">提供一个免费支持Jabber, AIM, MSN, Yahoo, Gtalk,Twitter 机器人</a></li>
<li><a href="http://js8.in/490.html" title="使用CSS设置文字反选的背景颜色" rel="bookmark inlinks">使用CSS设置文字反选的背景颜色</a></li>
<li><a href="http://js8.in/685.html" title="[转]利用跨域资源共享（CORS）实现ajax跨域调用" rel="bookmark inlinks">[转]利用跨域资源共享（CORS）实现ajax跨域调用</a></li>
<li><a href="http://js8.in/502.html" title="jQuery插件--wBox发布" rel="bookmark inlinks">jQuery插件&#8211;wBox发布</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/851.html#comments">2 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/%e4%bd%9c%e7%94%a8%e5%9f%9f" rel="tag">作用域</a>, <a href="http://js8.in/tag/%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1" rel="tag">面向对象</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="javascript作用域和作用域链" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F875.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10981959.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript作用域和作用域链</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript的词法作用域" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F745.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/10/23/9665591.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript的词法作用域</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript继承的写法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F698.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/08/02/20576222.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript继承的写法</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F851.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/851.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>javascript嵌套函数的效率问题</title>
		<link>http://js8.in/809.html</link>
		<comments>http://js8.in/809.html#comments</comments>
		<pubDate>Thu, 20 Oct 2011 05:27:00 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[前端优化]]></category>
		<category><![CDATA[嵌套函数]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=809</guid>
		<description><![CDATA[javascript自诞生以来就是一门受争议的编程语言，很多人也对javascript的语法表示不解，例如javascript嵌套函数。本文来自Nettuts+的一篇教程，详细的介绍了javascript中嵌套函数效率问题，从小处说起，一直说到匿名函数、继承，感觉不错。
嵌套函数效率
很多jser喜欢在javascript代码中使用嵌套函数，例如下面的例子就是一个典型的嵌套函数：

function foo&#40;a, b&#41; &#123;  
    function bar&#40;&#41; &#123;  
        return a + b;  
    &#125;  
&#160;
    return bar&#40;&#41;;  
&#125;  
&#160;
foo&#40;1, 2&#41;;

上面的代码中foo()中嵌入了bar()，当foo()运行的时候，就会调用bar()。javascript引擎不会创建bar()函数，直到外部引用了foo()，随着foo()的运行结束，bar()也会销毁。
当多次运行foo的时候，javascript引擎就要在每次的运行foo时创建bar函数，而每次foo结束就要销毁bar函数，这是一个很费劲的工作。
那么为什么我们不把bar函数拿出来，做为一个独立的函数，它在foo外部只被创建一次，而不是多次，这样就大大的提高了代码效率。例如下面的代码：

function foo&#40;a, b&#41; &#123;  
    return bar&#40;a, b&#41;;  [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="如何写出高质量的Javascript代码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F676.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979856.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">如何写出高质量的Javascript代码</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript匿名函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F722.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10984367.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript匿名函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript事件：获取事件对象getEvent函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F703.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979281.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript事件：获取事件对象getEvent函数</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>javascript自诞生以来就是一门受争议的编程语言，很多人也对javascript的语法表示不解，例如javascript嵌套函数。本文来自Nettuts+的一篇教程，详细的介绍了<strong>javascript</strong>中嵌套函数效率问题，从小处说起，一直说到匿名函数、继承，感觉不错。</p>
<h2>嵌套函数效率</h2>
<p>很多jser喜欢在javascript代码中使用嵌套函数，例如下面的例子就是一个典型的嵌套函数：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #003366; font-weight: bold;">function</span> bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #000066; font-weight: bold;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span>  
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>上面的代码中<code>foo()</code>中嵌入了<code>bar()</code>，当<code>foo()</code>运行的时候，就会调用<code>bar()</code>。javascript引擎不会创建<code>bar()</code>函数，直到外部引用了<code>foo()</code>，随着<code>foo()</code>的运行结束，<code>bar()</code>也会销毁。</p>
<p>当多次运行<code>foo</code>的时候，<strong>javascript引擎</strong>就要在每次的运行<code>foo</code>时创建<code>bar</code>函数，而每次<code>foo</code>结束就要销毁<code>bar</code>函数，这是一个很费劲的工作。</p>
<p>那么为什么我们不把<code>bar</code>函数拿出来，做为一个独立的函数，它在<code>foo</code>外部只被创建一次，而不是多次，这样就大大的提高了代码效率。例如下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">return</span> bar<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> bar<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>当然这样做可能随着程序的复杂性，可能存在命名冲突的危险，所以jser需要在这方面权衡，或者采用命名空间来解决这个方式。下面是在jsperf中做的关于上面两个函数大量运行的速度测试<a target="_blank" href="http://jsperf.com/nested-named-functions" target="_blank">http://jsperf.com/nested-named-functions</a>。不同的浏览器测试的结果不同，但是总体来看，两个独立的函数要比相互嵌套的javascript函数效率提高10%~90%。<br />
<span id="more-809"></span></p>
<h2>匿名函数</h2>
<p>javascript开发中常用到匿名函数，例如事件处理函数、callback函数等，例如下面的事件处理函数：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You clicked the page.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这里给document创建了一个事件监听，当每次页面点击之后会alert出来一条消息。跟嵌套函数一样，每次点击需要运行一次匿名函数，处理事件完成之后再销毁。</p>
<p>jQuery中的each方法也是一个<strong>匿名函数</strong>，例如下面的代码，选择出来所有的a元素，并且添加each方法来处理a元素：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果写成jQuery插件，可以下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">myPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
        <span style="color: #003366; font-weight: bold;">function</span> changeColor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
            $this.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>color <span style="color: #339933;">:</span> options.<span style="color: #660066;">color</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
        <span style="color: #009900;">&#125;</span>   
&nbsp;
        changeColor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>javascript代码定义了一个名字为myPlugin的jQuery插件，插件中有一个嵌套函数changeColor，根据上面说的，上面的代码效率不如独立出来changeColor高，所以我们可以把changeColor拿到外部来，即下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> changeColor<span style="color: #009900;">&#40;</span>$obj<span style="color: #339933;">,</span> color<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    $obj.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>color <span style="color: #339933;">:</span> color<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">myPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
        changeColor<span style="color: #009900;">&#40;</span>$this<span style="color: #339933;">,</span> options.<span style="color: #660066;">color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>经过修改过的jQuery插件在效率上提高了15%左右，大家可以通过<a target="_blank" href="http://jsperf.com/function-nesting-with-jquery-plugin" target="_blank">jsperf</a>来测试两个jQuery插件的效率。所以说嵌套的函数越多，调用的次数越多，则可以优化的地方也越多。</p>
<h2>javascript嵌套函数和函数构造</h2>
<p>我们在javascript类中长写下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span>firstName<span style="color: #339933;">,</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span>  
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getFullName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstName</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lastName</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> jeremy <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Jeremy&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;McPeak&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>  
    jeffrey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Jeffrey&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Way&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这段代码定义了一个Person的类，其中包括了<code>getFullName</code>的方法，将firstName和lastName返回。<code>getFullName</code>的方法在每次创建不同的Person对象时会不同，所以<code>jeremy.getFullName === jeffrey.getFullName</code>返回的结果是<code>false</code>(<a target="_blank" href="http://jsfiddle.net/k9uRN/" target="_blank">http://jsfiddle.net/k9uRN/</a>).<br />
具体分析见下面图，jeremy和jeffrey是不同的两个对象，他们的getFullName也是不同的。</p>
<div id="attachment_810" class="wp-caption aligncenter" style="width: 310px"><a href="http://js8.in/809.html/js_func_fig1" rel="attachment wp-att-810"><img src="http://js8.in/wp-content/uploads/2011/10/js_func_fig1-300x240.gif" alt="jeremy和jeffrey" title="jeremy和jeffrey" width="300" height="240" class="size-medium wp-image-810" /></a><p class="wp-caption-text">jeremy和jeffrey</p></div>
<h3>使用prototype关键字</h3>
<p>在javascript中有<strong>prototype</strong>这个关键字，<code>prototype</code>的属性是实例化后的对象所共有的属性，所以上面的代码可以通过prototype改写成下面的方式：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Person<span style="color: #009900;">&#40;</span>firstName<span style="color: #339933;">,</span> lastName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span>  
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
Person.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getFullName</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firstName</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lastName</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> jeremy <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Jeremy&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;McPeak&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>  
    jeffrey <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Jeffrey&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Way&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这样<code>getFullName</code>的方法是定义在<code>Person.prototype</code>中的，为所有实例化的对象共有方法，所以jeremy和jeffrey的<code>getFullName</code>是相等的(<a target="_blank" href="http://jsfiddle.net/Pfkua/" target="_blank">http://jsfiddle.net/Pfkua/</a>)。他们之间的关系可以通过下面的图片来解释：<br />
<div id="attachment_811" class="wp-caption aligncenter" style="width: 310px"><a href="http://js8.in/809.html/js_func_fig2" rel="attachment wp-att-811"><img src="http://js8.in/wp-content/uploads/2011/10/js_func_fig2-300x240.gif" alt="jeremy和jeffrey共有getFullName" title="jeremy和jeffrey" width="300" height="240" class="size-medium wp-image-811" /></a><p class="wp-caption-text">jeremy和jeffrey共有getFullName</p></div></p>
<p>通过<a target="_blank" href="http://jsperf.com/prototype-vs-non-prototype" target="_blank">jsPerf</a>的测试，我们可以看出来，第二种方法要比第一种方法在效率上面快了18%~96%。</p>
<h3>变量的私有化</h3>
<p>在函数内部的变量是私有的，外面是不可以访问到函数内部的变量的，但是函数内部可以访问到外部的变量。看下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Foo<span style="color: #009900;">&#40;</span>paramOne<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #003366; font-weight: bold;">var</span> thisIsPrivate <span style="color: #339933;">=</span> paramOne<span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bar</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #000066; font-weight: bold;">return</span> thisIsPrivate<span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello, Privacy!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">bar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts &quot;Hello, Privacy!&quot;</span></pre></div></div>

<p>代码中创建了一个构造函数<code>Foo();</code>，并且私有了一个变量<code>thisIsPrivate</code>，当运行<code>bar()</code>时，私有的<code>thisIsPrivate</code>会被返回。这样<code>thisIsPrivate</code>受到了保护，在<code>Foo()</code>之外是访问不到的.</p>
<p>这种方法也是很多javascript工程师所推荐的写法，但是跟上面的代码一样，每次实例化<code>Foo();</code>之时，会创建一个<code>bar</code>方法，这样看上去又是对资源的浪费，而且会影响效率。所以我们可以通过使用prototype的方法来实现：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Foo<span style="color: #009900;">&#40;</span>paramOne<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">this</span>._thisIsPrivate <span style="color: #339933;">=</span> paramOne<span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span>  
&nbsp;
Foo.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">bar</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._thisIsPrivate<span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello, Convention to Denote Privacy!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">bar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts &quot;Hello, Convention to Denote Privacy!&quot;</span></pre></div></div>

<p>这样的代码有不可以保证变量的私有化，只是我们在变量之前添加下划线<code>_</code>（很多公司内部规定，或者已经成为了很多程序员的编程习惯，_开头的变量是私有的），这样每次实例化<code>Foo();</code>会只建立一个通用的<code>bar</code>方法。</p>
<h2>总结</h2>
<p>本文也不是说不要大家在<strong>javascript</strong>中写嵌套函数，只是要适当，要注意这个知识点，在频繁调用的函数内部是不推荐写javascript嵌套函数的。开发者写代码给用户用，为的就是高效代码提高用户体验。</p>
<p>英文全文：http://net.tutsplus.com/tutorials/javascript-ajax/stop-nesting-functions-but-not-all-of-them
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/809.html" title="javascript嵌套函数的效率问题">http://js8.in/809.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/475.html" title="PHP二维数组去重复项函数" rel="bookmark inlinks">PHP二维数组去重复项函数</a></li>
<li><a href="http://js8.in/374.html" title="发布WordPress来访者天气预报插件" rel="bookmark inlinks">发布WordPress来访者天气预报插件</a></li>
<li><a href="http://js8.in/498.html" title="jQuery实现拖拽功能" rel="bookmark inlinks">jQuery实现拖拽功能</a></li>
<li><a href="http://js8.in/446.html" title="谷歌要退出中国大陆市场？！" rel="bookmark inlinks">谷歌要退出中国大陆市场？！</a></li>
<li><a href="http://js8.in/686.html" title="IE6下javascript不能隐藏select标签的bug" rel="bookmark inlinks">IE6下javascript不能隐藏select标签的bug</a></li>
<li><a href="http://js8.in/539.html" title="jQuery弹出框插件wBox 1.0正式发布" rel="bookmark inlinks">jQuery弹出框插件wBox 1.0正式发布</a></li>
<li><a href="http://js8.in/585.html" title="jQuery弹出框wBox的一个使用实例" rel="bookmark inlinks">jQuery弹出框wBox的一个使用实例</a></li>
<li><a href="http://js8.in/626.html" title="爆料：其实淘宝团购的聚划算每天有18款" rel="bookmark inlinks">爆料：其实淘宝团购的聚划算每天有18款</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/809.html#comments">13 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/%e5%89%8d%e7%ab%af%e4%bc%98%e5%8c%96" rel="tag">前端优化</a>, <a href="http://js8.in/tag/%e5%b5%8c%e5%a5%97%e5%87%bd%e6%95%b0" rel="tag">嵌套函数</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="如何写出高质量的Javascript代码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F676.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979856.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">如何写出高质量的Javascript代码</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript匿名函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F722.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10984367.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript匿名函数</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="javascript事件：获取事件对象getEvent函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F703.html&from=http%3A%2F%2Fjs8.in%2F809.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10979281.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">javascript事件：获取事件对象getEvent函数</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/809.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>HTMl5的sessionStorage和localStorage</title>
		<link>http://js8.in/802.html</link>
		<comments>http://js8.in/802.html#comments</comments>
		<pubDate>Wed, 19 Oct 2011 06:05:53 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[localStorage]]></category>
		<category><![CDATA[sessionStorage]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=802</guid>
		<description><![CDATA[html5中的Web Storage包括了两种存储方式：sessionStorage和localStorage。
sessionStorage用于本地存储一个会话（session）中的数据，这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁。因此sessionStorage不是一种持久化的本地存储，仅仅是会话级别的存储。
而localStorage用于持久化的本地存储，除非主动删除数据，否则数据是永远不会过期的。
web storage和cookie的区别
Web Storage的概念和cookie相似，区别是它是为了更大容量存储设计的。Cookie的大小是受限的，并且每次你请求一个新的页面的时候Cookie都会被发送过去，这样无形中浪费了带宽，另外cookie还需要指定作用域，不可以跨域调用。
除此之外，Web Storage拥有setItem,getItem,removeItem,clear等方法，不像cookie需要前端开发者自己封装setCookie，getCookie。
但是Cookie也是不可以或缺的：Cookie的作用是与服务器进行交互，作为HTTP规范的一部分而存在 ，而Web Storage仅仅是为了在本地“存储”数据而生（来自@otakustay 的纠正）
html5 web storage的浏览器支持情况
浏览器的支持除了IE７及以下不支持外，其他标准浏览器都完全支持(ie及FF需在web服务器里运行)，值得一提的是IE总是办好事，例如IE7、IE6中的UserData其实就是javascript本地存储的解决方案。通过简单的代码封装可以统一到所有的浏览器都支持web storage。
要判断浏览器是否支持localStorage可以使用下面的代码：

if&#40;window.localStorage&#41;&#123;
    alert&#40;&#34;浏览支持localStorage&#34;&#41;
&#125;else&#123;
   alert&#40;&#34;浏览暂不支持localStorage&#34;&#41;
&#125;
//或者
if&#40;typeof window.localStorage == 'undefined'&#41;&#123;
	alert&#40;&#34;浏览暂不支持localStorage&#34;&#41;
&#125;

localStorage和sessionStorage操作
localStorage和sessionStorage都具有相同的操作方法，例如setItem、getItem和removeItem等
localStorage和sessionStorage的方法
setItem存储value
用途：将value存储到key字段
用法：.setItem( key, value)
代码示例：

	sessionStorage.setItem&#40;&#34;key&#34;, &#34;value&#34;&#41;;
	localStorage.setItem&#40;&#34;site&#34;, &#34;js8.in&#34;&#41;;


getItem获取value
用途：获取指定key本地存储的值
用法：.getItem(key)
代码示例：

	var value = sessionStorage.getItem&#40;&#34;key&#34;&#41;;
	var site = localStorage.getItem&#40;&#34;site&#34;&#41;;

removeItem删除key
用途：删除指定key本地存储的值
用法：.removeItem(key)
代码示例：

	sessionStorage.removeItem&#40;&#34;key&#34;&#41;;
	localStorage.removeItem&#40;&#34;site&#34;&#41;;

clear清除所有的key/value
用途：清除所有的key/value
用法：.clear()
代码示例：

	sessionStorage.clear&#40;&#41;;
	localStorage.clear&#40;&#41;;

其他操作方法：点操作和[]
web Storage不但可以用自身的setItem,getItem等方便存取，也可以像普通对象一样用点(.)操作符，及[]的方式进行数据存储，像如下的代码：

var storage = window.localStorage;
storage.key1 = &#34;hello&#34;;
storage&#91;&#34;key2&#34;&#93; = &#34;world&#34;;
console.log&#40;storage.key1&#41;;
console.log&#40;storage&#91;&#34;key2&#34;&#93;&#41;;

localStorage和sessionStorage的key和length属性实现遍历
sessionStorage和localStorage提供的key()和length可以方便的实现存储的数据遍历，例如下面的代码：

var storage = window.localStorage;
for &#40;var i=0, len = storage.length; i  &#60;  len; i++&#41;&#123;
   [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Javascript事件触发列表与详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10982510.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript事件触发列表与详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="7个JavaScript在IE和Firefox浏览器下的差异写法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F426.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10990016.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">7个JavaScript在IE和Firefox浏览器下的差异写法</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript正则表达式详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/06/14873015.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript正则表达式详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>html5中的Web Storage包括了两种存储方式：sessionStorage和localStorage。</p>
<p><strong>sessionStorage</strong>用于本地存储一个会话（session）中的数据，这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁。因此sessionStorage不是一种持久化的本地存储，仅仅是会话级别的存储。</p>
<p>而<strong>localStorage</strong>用于持久化的本地存储，除非主动删除数据，否则数据是永远不会过期的。</p>
<h2>web storage和cookie的区别</h2>
<p>Web Storage的概念和cookie相似，区别是它是为了更大容量存储设计的。Cookie的大小是受限的，并且每次你请求一个新的页面的时候Cookie都会被发送过去，这样无形中浪费了带宽，另外cookie还需要指定作用域，不可以跨域调用。</p>
<p>除此之外，<strong>Web Storage</strong>拥有setItem,getItem,removeItem,clear等方法，不像cookie需要前端开发者自己封装setCookie，getCookie。</p>
<p>但是Cookie也是不可以或缺的：Cookie的作用是与服务器进行交互，作为HTTP规范的一部分而存在 ，而Web Storage仅仅是为了在本地“存储”数据而生（来自@otakustay 的纠正）</p>
<h2>html5 web storage的浏览器支持情况</h2>
<p>浏览器的支持除了IE７及以下不支持外，其他标准浏览器都完全支持(ie及FF需在web服务器里运行)，值得一提的是IE总是办好事，例如IE7、IE6中的UserData其实就是<strong>javascript本地存储</strong>的解决方案。通过简单的代码封装可以统一到所有的浏览器都支持web storage。</p>
<p>要判断浏览器是否支持localStorage可以使用下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">localStorage</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;浏览支持localStorage&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;浏览暂不支持localStorage&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//或者</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #660066;">localStorage</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;浏览暂不支持localStorage&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>localStorage和sessionStorage操作</h2>
<p>localStorage和sessionStorage都具有相同的操作方法，例如setItem、getItem和removeItem等</p>
<h3>localStorage和sessionStorage的方法</h3>
<h4>setItem存储value</h4>
<p>用途：将value存储到key字段<br />
用法：.setItem( key, value)<br />
代码示例：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	sessionStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;key&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;site&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;js8.in&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-802"></span></p>
<h4>getItem获取value</h4>
<p>用途：获取指定key本地存储的值<br />
用法：.getItem(key)<br />
代码示例：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">var</span> value <span style="color: #339933;">=</span> sessionStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;key&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> site <span style="color: #339933;">=</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;site&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>removeItem删除key</h4>
<p>用途：删除指定key本地存储的值<br />
用法：.removeItem(key)<br />
代码示例：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	sessionStorage.<span style="color: #660066;">removeItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;key&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	localStorage.<span style="color: #660066;">removeItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;site&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>clear清除所有的key/value</h4>
<p>用途：清除所有的key/value<br />
用法：.clear()<br />
代码示例：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	sessionStorage.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	localStorage.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>其他操作方法：点操作和[]</h4>
<p>web Storage不但可以用自身的setItem,getItem等方便存取，也可以像普通对象一样用点(.)操作符，及[]的方式进行数据存储，像如下的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> storage <span style="color: #339933;">=</span> window.<span style="color: #660066;">localStorage</span><span style="color: #339933;">;</span>
storage.<span style="color: #660066;">key1</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
storage<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;key2&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;world&quot;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>storage.<span style="color: #660066;">key1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>storage<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;key2&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>localStorage和sessionStorage的key和length属性实现遍历</h4>
<p>sessionStorage和localStorage提供的key()和length可以方便的实现存储的数据遍历，例如下面的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> storage <span style="color: #339933;">=</span> window.<span style="color: #660066;">localStorage</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len <span style="color: #339933;">=</span> storage.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i  <span style="color: #339933;">&lt;</span>  len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> storage.<span style="color: #660066;">key</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> value <span style="color: #339933;">=</span> storage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>storage事件</h3>
<p>storage还提供了<strong>storage事件</strong>，当键值改变或者clear的时候，就可以触发storage事件，如下面的代码就添加了一个storage事件改变的监听：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;storage&quot;</span><span style="color: #339933;">,</span>handle_storage<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	window.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onstorage&quot;</span><span style="color: #339933;">,</span>handle_storage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> handle_storage<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>e<span style="color: #339933;">=</span>window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>storage事件对象的具体属性如下表：</p>
<table width="100%" border="1">
<tr>
<td> Property</td>
<td> Type</td>
<td> Description</td>
</tr>
<tr>
<td>key</td>
<td>String</td>
<td> The named key that was added, removed, or moddified</td>
</tr>
<tr>
<td>oldValue</td>
<td>	 Any</td>
<td> The previous value(now overwritten), or null if a new item was added</td>
</tr>
<tr>
<td>newValue</td>
<td>	 Any</td>
<td>	 The new value, or null if an item was added</td>
</tr>
<tr>
<td> url/uri</td>
<td>String </td>
<td>The page that called the method that triggered this change</td>
</tr>
</table>
<h2>Web Storage Demo</h2>
<p class="demoview">
<a target="_blank" href="http://html5demos.com/storage" target="_blank">HTML5 Demos: Storage</a> &nbsp;&nbsp;&nbsp;&nbsp;<a target="_blank" href="http://people.opera.com/shwetankd/external/demos/webstorage_demo.htm" target="_blank">Web Storage Example</a>
</p>
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/802.html" title="HTMl5的sessionStorage和localStorage">http://js8.in/802.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/436.html" title="三个使用WordPress自定义域做的小工具" rel="bookmark inlinks">三个使用WordPress自定义域做的小工具</a></li>
<li><a href="http://js8.in/342.html" title="php、js两种不同方式根据关键词返回经纬度接口【基于Google map API】" rel="bookmark inlinks">php、js两种不同方式根据关键词返回经纬度接口【基于Google map API】</a></li>
<li><a href="http://js8.in/490.html" title="使用CSS设置文字反选的背景颜色" rel="bookmark inlinks">使用CSS设置文字反选的背景颜色</a></li>
<li><a href="http://js8.in/501.html" title="IE中Image.onload失效的问题" rel="bookmark inlinks">IE中Image.onload失效的问题</a></li>
<li><a href="http://js8.in/289.html" title="用批处理说中文！" rel="bookmark inlinks">用批处理说中文！</a></li>
<li><a href="http://js8.in/542.html" title="通过修改注册表去掉windows7的快捷方式小箭头" rel="bookmark inlinks">通过修改注册表去掉windows7的快捷方式小箭头</a></li>
<li><a href="http://js8.in/674.html" title="使用xdebug调试PHP 找出PHP程序的瓶颈" rel="bookmark inlinks">使用xdebug调试PHP 找出PHP程序的瓶颈</a></li>
<li><a href="http://js8.in/593.html" title="解决IE下setTimeout传参数的bug" rel="bookmark inlinks">解决IE下setTimeout传参数的bug</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/802.html#comments">3 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/localstorage" rel="tag">localStorage</a>, <a href="http://js8.in/tag/sessionstorage" rel="tag">sessionStorage</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="Javascript事件触发列表与详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10982510.bmp" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript事件触发列表与详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="7个JavaScript在IE和Firefox浏览器下的差异写法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F426.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10990016.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">7个JavaScript在IE和Firefox浏览器下的差异写法</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Javascript正则表达式详解" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2012/02/06/14873015.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Javascript正则表达式详解</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="非字母javascript" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F918.html&from=http%3A%2F%2Fjs8.in%2F802.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/30/11698517.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">非字母javascript</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/802.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>基于express+socket.io的nodejs聊天室</title>
		<link>http://js8.in/792.html</link>
		<comments>http://js8.in/792.html#comments</comments>
		<pubDate>Fri, 30 Sep 2011 07:20:24 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=792</guid>
		<description><![CDATA[前几天晚上边看水浒边写的nodejs的聊天室，前面说了，放假之前要把近日学习nodejs的所有心得整理下，今天就是30号鸟~撒欢~，最后放这个聊天室出来给大家作为学习nodejs的参考示例，希望对大家有用。
感谢：cnodejs群里的老雷，及其微博上的基友们！顺祝大家长假快乐，顺祝自己明天动车不出轨，顺祝明年不再过节，感慨多了……
特点
聊天室主要功能及其特点：

采用nodejs（屁话）
express框架,jade做模板
socket.io做前后端的websocket通信
支持session
支持@私信功能

废话不多说了，注意点，基本前面的文章都提到了，下面罗列下：
《配置nodejs.exe的windows目录结构》
《安装express及配置app.js文件》
《使用socket.io和node.js搭建websocket应用》
《在Express和Socket.IO中使用session》
nodejs聊天室下载地址
基于express+socket.io的聊天室
聊天室服务器端js代码


//========================变量定义===============================
/**
 * modules引入
 */
var express = require&#40;'express'&#41;,
	sio = require&#40;'socket.io'&#41;,
	fs=require&#40;'fs'&#41;,
	path = require&#40;'path'&#41;
	url = require&#40;'url'&#41;,
	parseCookie = require&#40;'connect'&#41;.utils.parseCookie,
	MemoryStore = require&#40;'connect/middleware/session/memory'&#41;;
&#160;
/**
 * 私人聊天使用session
 */
var usersWS = &#123;&#125;, //私人聊天用的websocket
	storeMemory = new MemoryStore&#40;&#123;
		reapInterval: 60000 * 10
	&#125;&#41;;//session store
//=========================app配置=============================	
/**
 * app配置
 */
var app = module.export = express.createServer&#40;&#41;;
&#160;
app.configure&#40;function&#40;&#41;&#123;
	app.use&#40;express.bodyParser&#40;&#41;&#41;;
	app.use&#40;express.cookieParser&#40;&#41;&#41;;
	app.use&#40;express.session&#40;&#123;
		secret: 'wyq',
		store:storeMemory 
	&#125;&#41;&#41;;
	app.use&#40;express.methodOverride&#40;&#41;&#41;;
	app.use&#40;app.router&#41;;//要放在bodyParser之后，处理post
	app.set&#40;'views', __dirname + '/views'&#41;;
	app.set&#40;'view engine', 'jade'&#41;;
	app.use&#40;express.static&#40;__dirname + '/public'&#41;&#41;;
&#125;&#41;;
//=================配置socket.io=========================
/**
 * 配置socket.io
 * 
 */	
var io = [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="使用cygwin在windows下安装nodeJS" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F758.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/21/32572660.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">使用cygwin在windows下安装nodeJS</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="nodejs教程：配置nodejs.exe的windows目录结构" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F764.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978302.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">nodejs教程：配置nodejs.exe的windows目录结构</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="nodejs教程：安装express及配置app.js文件" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F774.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978526.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">nodejs教程：安装express及配置app.js文件</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="使用socket.io和node.js搭建websocket应用" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F784.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/15/10903638.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">使用socket.io和node.js搭建websocket应用</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>前几天晚上边看水浒边写的<a href="http://js8.in/tag/nodejs">nodejs</a>的聊天室，前面说了，放假之前要把近日学习nodejs的所有心得整理下，今天就是30号鸟~撒欢~，最后放这个聊天室出来给大家作为学习<strong>nodejs</strong>的参考示例，希望对大家有用。</p>
<p>感谢：cnodejs群里的老雷，及其<a target="_blank" href="http://weibo.com/sanshuiqing">微博</a>上的基友们！顺祝大家长假快乐，顺祝自己明天动车不出轨，顺祝明年不再过节，感慨多了……</p>
<h3>特点</h3>
<p>聊天室主要功能及其特点：</p>
<ol>
<li>采用nodejs（屁话）</li>
<li>express框架,jade做模板</li>
<li>socket.io做前后端的websocket通信</li>
<li>支持session</li>
<li>支持@私信功能</li>
</ol>
<p>废话不多说了，注意点，基本前面的文章都提到了，下面罗列下：<br />
《<a href="http://js8.in/764.html" target="_blank">配置nodejs.exe的windows目录结构</a>》<br />
《<a href="http://js8.in/774.html" target="_blank">安装express及配置app.js文件</a>》<br />
《<a href="http://js8.in/784.html" target="_blank">使用socket.io和node.js搭建websocket应用</a>》<br />
《<a href="http://js8.in/788.html" target="_blank">在Express和Socket.IO中使用session</a>》</p>
<h3>nodejs聊天室下载地址</h3>
<p class='demoview'><a target="_blank" href="http://1.nodejsdemo.sinaapp.com/chat/chat.zip" target="_blank" title="基于express+socket.io的聊天室">基于express+socket.io的聊天室</a></p>
<h3>聊天室服务器端js代码</h3>
<p><span id="more-792"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//========================变量定义===============================</span>
<span style="color: #006600; font-style: italic;">/**
 * modules引入
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> express <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'express'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	sio <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'socket.io'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	fs<span style="color: #339933;">=</span>require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	path <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'path'</span><span style="color: #009900;">&#41;</span>
	url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	parseCookie <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'connect'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">utils</span>.<span style="color: #660066;">parseCookie</span><span style="color: #339933;">,</span>
	MemoryStore <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'connect/middleware/session/memory'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * 私人聊天使用session
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> usersWS <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">//私人聊天用的websocket</span>
	storeMemory <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MemoryStore<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		reapInterval<span style="color: #339933;">:</span> <span style="color: #CC0000;">60000</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//session store</span>
<span style="color: #006600; font-style: italic;">//=========================app配置=============================	</span>
<span style="color: #006600; font-style: italic;">/**
 * app配置
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> app <span style="color: #339933;">=</span> module.<span style="color: #003366; font-weight: bold;">export</span> <span style="color: #339933;">=</span> express.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
app.<span style="color: #660066;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	app.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">bodyParser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	app.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">cookieParser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	app.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">session</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		secret<span style="color: #339933;">:</span> <span style="color: #3366CC;">'wyq'</span><span style="color: #339933;">,</span>
		store<span style="color: #339933;">:</span>storeMemory 
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	app.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">methodOverride</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	app.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">router</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//要放在bodyParser之后，处理post</span>
	app.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'views'</span><span style="color: #339933;">,</span> __dirname <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/views'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	app.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'view engine'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'jade'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	app.<span style="color: #003366; font-weight: bold;">use</span><span style="color: #009900;">&#40;</span>express.<span style="color: #660066;">static</span><span style="color: #009900;">&#40;</span>__dirname <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/public'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//=================配置socket.io=========================</span>
<span style="color: #006600; font-style: italic;">/**
 * 配置socket.io
 * 
 */</span>	
<span style="color: #003366; font-weight: bold;">var</span> io <span style="color: #339933;">=</span> sio.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>app<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//设置session</span>
io.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'authorization'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>handshakeData<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// 通过客户端的cookie字符串来获取其session数据</span>
	handshakeData.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> parseCookie<span style="color: #009900;">&#40;</span>handshakeData.<span style="color: #660066;">headers</span>.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#41;</span>
	<span style="color: #003366; font-weight: bold;">var</span> connect_sid <span style="color: #339933;">=</span> handshakeData.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'connect.sid'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connect_sid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		storeMemory.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>connect_sid<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> session<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// if we cannot grab a session, turn down the connection</span>
				callback<span style="color: #009900;">&#40;</span>error.<span style="color: #660066;">message</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// save the session data and accept the connection</span>
				handshakeData.<span style="color: #660066;">session</span> <span style="color: #339933;">=</span> session<span style="color: #339933;">;</span>
				callback<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		callback<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'nosession'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//=========================URL=============================</span>
<span style="color: #006600; font-style: italic;">/**
 * url处理开始鸟~
 * @param {Object} req
 * @param {Object} res
 */</span>
app.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> req.<span style="color: #660066;">session</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">&amp;&amp;</span> req.<span style="color: #660066;">session</span>.<span style="color: #000066;">name</span><span style="color: #339933;">!==</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//需要判断下是否已经登录</span>
		res.<span style="color: #660066;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/chat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//读取登录页面，要求登录</span>
		<span style="color: #003366; font-weight: bold;">var</span> realpath <span style="color: #339933;">=</span> __dirname <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/views/'</span> <span style="color: #339933;">+</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login.html'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pathname</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> txt <span style="color: #339933;">=</span> fs.<span style="color: #660066;">readFileSync</span><span style="color: #009900;">&#40;</span>realpath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
app.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/chat'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">session</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">&amp;&amp;</span> req.<span style="color: #660066;">session</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//需要判断下是否已经登录</span>
		res.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chat'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span>req.<span style="color: #660066;">session</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		res.<span style="color: #660066;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
app.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/chat'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span>res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> req.<span style="color: #660066;">body</span>.<span style="color: #660066;">nick</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066;">name</span><span style="color: #339933;">!==</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		req.<span style="color: #660066;">session</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//设置session</span>
		res.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chat'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span><span style="color: #000066;">name</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'nickname cannot null'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//===================socket链接监听=================</span>
<span style="color: #006600; font-style: italic;">/**
 * 开始socket链接监听
 * @param {Object} socket
 */</span>
io.<span style="color: #660066;">sockets</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'connection'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>socket<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> session <span style="color: #339933;">=</span> socket.<span style="color: #660066;">handshake</span>.<span style="color: #660066;">session</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//session</span>
	<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> session.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
	usersWS<span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> socket<span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> refresh_online <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> n <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> usersWS<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			n.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		io.<span style="color: #660066;">sockets</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'online list'</span><span style="color: #339933;">,</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">//所有人广播</span>
	<span style="color: #009900;">&#125;</span>
	refresh_online<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	socket.<span style="color: #660066;">broadcast</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'system message'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'【'</span><span style="color: #339933;">+</span><span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'】回来了，大家赶紧去找TA聊聊~~'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//公共信息</span>
	socket.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'public message'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		socket.<span style="color: #660066;">broadcast</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'public message'</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		fn<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">//私人@信息</span>
	socket.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'private message'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>to<span style="color: #339933;">,</span> msg<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> usersWS<span style="color: #009900;">&#91;</span>to<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			fn<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			target.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'private message'</span><span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'[私信]'</span><span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			fn<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
			socket.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'message error'</span><span style="color: #339933;">,</span> to<span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//掉线，断开链接处理</span>
	socket.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disconnect'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">delete</span> usersWS<span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		session <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		socket.<span style="color: #660066;">broadcast</span>.<span style="color: #660066;">emit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'system message'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'【'</span><span style="color: #339933;">+</span><span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'】无声无息地离开了。。。'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		refresh_online<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//===========app listen 开始鸟~==========</span>
app.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3000</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> addr <span style="color: #339933;">=</span> app.<span style="color: #660066;">address</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app listening on http://127.0.0.1：'</span> <span style="color: #339933;">+</span> addr.<span style="color: #660066;">port</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/792.html" title="基于express+socket.io的nodejs聊天室">http://js8.in/792.html</a>. 转载请注明转自<a href="http://js8.in" title="前端开发博客"> JS8.IN ™</a></span></span></div>
<h3 class="box-title">随机文章</h3>
<ul class="box_random">
<li><a href="http://js8.in/383.html" title="GoDaddy促销COM域名，在华仅0.99美元" rel="bookmark inlinks">GoDaddy促销COM域名，在华仅0.99美元</a></li>
<li><a href="http://js8.in/900.html" title="开博两年了：换了主题 改了名" rel="bookmark inlinks">开博两年了：换了主题 改了名</a></li>
<li><a href="http://js8.in/627.html" title="新浪微博应用开发之一：授权机制介绍" rel="bookmark inlinks">新浪微博应用开发之一：授权机制介绍</a></li>
<li><a href="http://js8.in/378.html" title="樱花草，假如爱有天意" rel="bookmark inlinks">樱花草，假如爱有天意</a></li>
<li><a href="http://js8.in/622.html" title="IE中flash遮挡div现象的解决方案" rel="bookmark inlinks">IE中flash遮挡div现象的解决方案</a></li>
<li><a href="http://js8.in/395.html" title="Google2009年Logo大盘点——第二季" rel="bookmark inlinks">Google2009年Logo大盘点——第二季</a></li>
<li><a href="http://js8.in/585.html" title="jQuery弹出框wBox的一个使用实例" rel="bookmark inlinks">jQuery弹出框wBox的一个使用实例</a></li>
<li><a href="http://js8.in/469.html" title="jQuery 1.4.1 中文参考文档下载" rel="bookmark inlinks">jQuery 1.4.1 中文参考文档下载</a></li>
</ul>
<p style="margin:0;padding:0;height:1px;overflow:hidden;">
    <script type="text/javascript"><!--
        var wumiiSitePrefix = "http://js8.in";
        var wumiiEnableCustomPos = false;
        var wumiiParams = "&#038;num=4&#038;mode=3&#038;displayInFeed=1&#038;version=1.0.5.5&#038;pf=WordPress2.8.6";
    //--></script></p>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2011. |
<a href="http://js8.in/792.html#comments">6 comments</a> |在微博关注我：<a href="//weibo.com/sanshuiqing" target="_blank" title="关注断桥残雪的新浪微博">@清-三水清</a>
<br/>收藏到：
<a rel="nofollow" href="http://share.renren.com/share/buttonshare.do?link=%posturl%&title=%posttitle%" title="收藏本文到人人网" target="_blank">人人网</a> | <a rel="nofollow" href="http://www.douban.com/recommend/?url=%posturl%&title=%posttitle%" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=%posttitle%&rurl=%posturl%" title="收藏本文到开心网" target="_blank">开心网</a> | <a rel="nofollow" href="http://shuqian.qq.com/post?title=%posttitle%&%20uri=%posturl%" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a rel="nofollow" href="http://v.t.sina.com.cn/share/share.php?url=%posturl%&title=%posttitle%" title="收藏本文到新浪微博" target="_blank">新浪微博</a> | <a rel="nofollow" href="http://v.t.qq.com/share/share.php?title=%posttitle%&url=%posturl%&site=http://www.kaixin100.info" title="收藏本文到腾讯微博" target="_blank">腾讯微博</a>
<br/>
Post tags: <a href="http://js8.in/tag/javascript" rel="tag">javascript</a>, <a href="http://js8.in/tag/nodejs" rel="tag">nodejs</a><br/>
</small></p><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">猜你喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="使用cygwin在windows下安装nodeJS" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F758.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/09/21/32572660.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">使用cygwin在windows下安装nodeJS</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="nodejs教程：配置nodejs.exe的windows目录结构" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F764.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978302.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">nodejs教程：配置nodejs.exe的windows目录结构</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="nodejs教程：安装express及配置app.js文件" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F774.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/17/10978526.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">nodejs教程：安装express及配置app.js文件</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="使用socket.io和node.js搭建websocket应用" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fjs8.in%2F784.html&from=http%3A%2F%2Fjs8.in%2F792.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/11/15/10903638.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">使用socket.io和node.js搭建websocket应用</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://js8.in/792.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

