<?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>Sat, 21 Apr 2012 02:50:09 +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>nodePPT — nodejs做的手机端控制PC端的PPT演示文档javascript框架</title>
		<link>http://js8.in/989.html</link>
		<comments>http://js8.in/989.html#comments</comments>
		<pubDate>Sat, 21 Apr 2012 02:47:01 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[web前端开发]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[PPT]]></category>

		<guid isPermaLink="false">http://js8.in/?p=989</guid>
		<description><![CDATA[nodePPT使用nodejs写的，可以手机端控制pc端的演示文档框架。nodePPT是在webSlide基础上二次开发的，使用nodejs做服务器，通过websocket来实现手机端和PC的通信，主要功能是手机端可以控制pc端PPT播放进度，并且手机端可以给每张PPT做备注。
第一次做个程序的nodejs作品，并且测试时间比较短，肯定会有很多问题和bug，希望大家一起斧正。另外页面样式css高手可以自己写，包括动画自定义都可以在css文件中直接修改添加。
说明
因为是PPT演示文档，所以需要投影仪分辨率，所以演示页面的最佳分辨率为全屏模式下的 1024*768 ，如果在自己电脑上查看，可以通过 ctrl + - 和 ctrl + + 缩放到合适的比例查看效果。
建议浏览器chrome 16+，全屏模式（F11），以达到最佳动画效果。在Firefox下会出现拖尾现象，个人认为是Firefox 8下对CSS3动画效果渲染太慢导致，其他浏览器未测试。
使用说明

配置route.json
配置ppt的文件
启动node：node server.js
PC访问：如127.0.0.1:3000/demo.ppt
手机端访问：127.0.0.1:3000/ctrl
手机端setup，选择控制用户后，开始控制

具体使用方法启动node server.js之后访问127.0.0.1:3000/index.ppt，里面有详细的介绍
快捷键

空格/→/↓/Tab/pageDown：下一页
←/↑/pageUp：上一页
P：开画板
C：清除画板

版本库地址

支持三种访问协议：

HTTP协议： https://ksky521@github.com/ksky521/nodePPT.git 。
Git协议： git://github.com/ksky521/nodePPT.git 。
SSH协议： ssh://git@github.com:ksky521/nodePPT.git 。

克隆版本库
操作示例：
$ git clone git://github.com/ksky521/nodePPT.git

联系方式
作者博客：js8.in
作者新浪微博：@三水清

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

javascript作用域和作用域链
使用滔滔给WordPress添加博主状态
cURL常用的几个PHP函数
使用xdebug调试PHP 找出PHP程序的瓶颈
光良,第一次—珍喜欢
查看自己是否在对方QQ好友中最简单的方法
【转】解析360和QQ的战争—《“听说，250干110的事儿，那就是360了”》
哎~都挺穷的！


© 三水清 for 三水清, 2012. &#124;
3 comments &#124;在微博关注我：@清-三水清
收藏到：
人人网 &#124; 豆瓣 &#124; 开心网 &#124; QQ书签 &#124; 新浪微博 &#124; 腾讯微博

Post tags: javascript, nodejs, PPT
<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="nodejs教程：配置nodejs.exe的windows目录结构" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F764.html&from=http%3A%2F%2Fjs8.in%2F989.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?url=http%3A%2F%2Fjs8.in%2F774.html&from=http%3A%2F%2Fjs8.in%2F989.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="使用cygwin在windows下安装nodeJS" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F758.html&from=http%3A%2F%2Fjs8.in%2F989.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="javascript入门到高级PPT" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F750.html&from=http%3A%2F%2Fjs8.in%2F989.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/10979311.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入门到高级PPT</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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 target="_blank" href="https://github.com/ksky521/nodePPT">nodePPT</a>使用nodejs写的，可以手机端控制pc端的演示文档框架。<strong>nodePPT</strong>是在<a target="_blank" href="https://github.com/ksky521/webSlide">webSlide</a>基础上二次开发的，使用nodejs做服务器，通过websocket来实现手机端和PC的通信，主要功能是手机端可以控制pc端PPT播放进度，并且手机端可以给每张PPT做备注。</p>
<p>第一次做个程序的<strong>nodejs</strong>作品，并且测试时间比较短，肯定会有很多问题和bug，希望大家一起斧正。另外页面样式css高手可以自己写，包括动画自定义都可以在css文件中直接修改添加。</p>
<h2>说明</h2>
<p>因为是PPT演示文档，所以需要投影仪分辨率，所以演示页面的最佳分辨率为全屏模式下的 <code>1024*768</code> ，如果在自己电脑上查看，可以通过 <code>ctrl + -</code> 和 <code>ctrl + +</code> 缩放到合适的比例查看效果。</p>
<p>建议浏览器chrome 16+，全屏模式（F11），以达到最佳动画效果。在Firefox下会出现拖尾现象，个人认为是Firefox 8下对CSS3动画效果渲染太慢导致，其他浏览器未测试。<br />
<div id="attachment_995" class="wp-caption aligncenter" style="width: 450px"><a href="http://js8.in/989.html/796f423btw1ds70hjcu93j" rel="attachment wp-att-995"><img src="http://js8.in/wp-content/uploads/2012/04/796f423btw1ds70hjcu93j.jpg" alt="nodePPT iphone手机端截图" title="nodePPT iphone手机端截图" width="440" height="616" class="size-full wp-image-995" /></a><p class="wp-caption-text">nodePPT iphone手机端截图</p></div></p>
<h4>使用说明</h4>
<ol>
<li>配置route.json</li>
<li>配置ppt的文件</li>
<li>启动node：node server.js</li>
<li>PC访问：如127.0.0.1:3000/demo.ppt</li>
<li>手机端访问：127.0.0.1:3000/ctrl</li>
<li>手机端setup，选择控制用户后，开始控制</li>
</ol>
<p>具体使用方法启动node server.js之后访问127.0.0.1:3000/index.ppt，里面有详细的介绍</p>
<h2>快捷键</h2>
<ul>
<li>空格/→/↓/Tab/pageDown：下一页</li>
<li>←/↑/pageUp：上一页</li>
<li>P：开画板</li>
<li>C：清除画板</li>
</ul>
<h2>版本库地址</h2>
<p><span id="more-989"></span><br />
支持三种访问协议：</p>
<ol>
<li>HTTP协议： <code>https://ksky521@github.com/ksky521/nodePPT.git</code> 。</li>
<li>Git协议： <code>git://github.com/ksky521/nodePPT.git</code> 。</li>
<li>SSH协议： <code>ssh://git@github.com:ksky521/nodePPT.git</code> 。</li>
</ol>
<h2>克隆版本库</h2>
<p>操作示例：</p>
<pre><code>$ git clone git://github.com/ksky521/nodePPT.git
</code></pre>
<h2>联系方式</h2>
<p>作者博客：<a href="http://js8.in">js8.in</a></p>
<p>作者新浪微博：<a target="_blank" href="http://weibo.com/sanshuiqing">@三水清</a>
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/989.html" title="nodePPT — nodejs做的手机端控制PC端的PPT演示文档javascript框架">http://js8.in/989.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/951.html" title="【PPT分享】XSS注入和防范" rel="bookmark inlinks">【PPT分享】XSS注入和防范</a></li>
<li><a href="http://js8.in/289.html" title="用批处理说中文！" rel="bookmark inlinks">用批处理说中文！</a></li>
<li><a href="http://js8.in/467.html" title="Javascript设置光标位置到结尾并显示出来" rel="bookmark inlinks">Javascript设置光标位置到结尾并显示出来</a></li>
<li><a href="http://js8.in/499.html" title="QQ爱墙 许愿墙 祝福墙 程序源代码开发下载" rel="bookmark inlinks">QQ爱墙 许愿墙 祝福墙 程序源代码开发下载</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/564.html" title="Google Map API教程之使用GControl对象在地图上添加control按钮" rel="bookmark inlinks">Google Map API教程之使用GControl对象在地图上添加control按钮</a></li>
<li><a href="http://js8.in/335.html" title="Sql语句优化注意" rel="bookmark inlinks">Sql语句优化注意</a></li>
<li><a href="http://js8.in/745.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/989.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/nodejs" rel="tag">nodejs</a>, <a href="http://js8.in/tag/ppt" rel="tag">PPT</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="nodejs教程：配置nodejs.exe的windows目录结构" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F764.html&from=http%3A%2F%2Fjs8.in%2F989.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?url=http%3A%2F%2Fjs8.in%2F774.html&from=http%3A%2F%2Fjs8.in%2F989.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="使用cygwin在windows下安装nodeJS" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F758.html&from=http%3A%2F%2Fjs8.in%2F989.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="javascript入门到高级PPT" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F750.html&from=http%3A%2F%2Fjs8.in%2F989.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/10979311.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入门到高级PPT</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/989.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>addEventListener第二个参数的handleEvent</title>
		<link>http://js8.in/983.html</link>
		<comments>http://js8.in/983.html#comments</comments>
		<pubDate>Thu, 08 Mar 2012 02:48:17 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://js8.in/?p=983</guid>
		<description><![CDATA[昨天无意中看到一篇老外的文章，文章提到了addEventListener第二个参数可以传入object，并且对象中handleEvent为事件处理函数。例如下面的javascript代码：

var obj = &#123;
	handleEvent: function&#40;&#41; &#123;
		alert&#40;'body clicked'&#41;;
	&#125;
&#125;;
document.body.addEventListener&#40;'click',obj,false&#41;;

测试发现这段代码在IE9+，chrome，FF，opera等浏览器是正确的（测试范围可能不够宽），而且可以成功的绑定click事件。在Qwrap群里一问才知道是DOM2中接口，很有意思的一个发现，@貘大大 也发微博说自己先入为主了，我相信很多人跟@貘大大 一样都先入为主了。
addEventListener的handleEvent应用
发现了这个“新大陆”有什么用法呢？哈哈~首先想到的是对象引用。看下面的javascript代码：

&#60;script type=&#34;text/javascript&#34;&#62;
var obj = {
handleEvent: function() {
	alert('body clicked');
}
};
document.body.addEventListener('click',obj,false);
function fn2(){
	obj.handleEvent = function(){
		alert('fn2');
	};
}
&#60;/script&#62;
&#60;button onclick=&#34;fn2();&#34;&#62;change&#60;/button&#62;

因为对象的引用关系，点击下button就可以切换绑定事件的处理函数，而不需要remove之前的事件，然后重新绑定一个新的处理函数。

如果你觉得这个不够炫，那么下面的javascript代码可能就更有吸引力了哦~

var obj2 = &#123;
	addEvent:function&#40;el&#41;&#123;
		el.addEventListener&#40;'click',this,false&#41;;
	&#125;,
	handleEvent:function&#40;e&#41;&#123;
		alert&#40;e.target&#41;;
	&#125;
&#125;;
obj2.addEvent&#40;document.body&#41;;

addEventListener handleEvent缺点
显而易见，IE6~IE8不能使用……可是如果是高级浏览器还是不错的功能哦~
扩展阅读
http://peter.michaux.ca/articles/our-backwards-dom-event-libraries
http://ajaxian.com/archives/an-alternative-way-to-addeventlistener

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

再来几个腾讯微博的邀请种子
Google Map API教程之使用GControl对象在地图上添加control按钮
javascript contains方法来判断元素包含关系
PHP生成RSS类：RSS.class.php
查看自己是否在对方QQ好友中最简单的方法
chrome字体最小值12px问题及解决方法
爆料：其实淘宝团购的聚划算每天有18款
练手：一个获取验证码图片到本地的PHP程序


© 三水清 for 三水清, 2012. &#124;
One comment &#124;在微博关注我：@清-三水清
收藏到：
人人网 &#124; 豆瓣 &#124; 开心网 &#124; QQ书签 &#124; 新浪微博 &#124; 腾讯微博

Post tags: addEventListener, 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?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F983.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?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F983.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?url=http%3A%2F%2Fjs8.in%2F529.html&from=http%3A%2F%2Fjs8.in%2F983.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?url=http%3A%2F%2Fjs8.in%2F408.html&from=http%3A%2F%2Fjs8.in%2F983.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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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 target="_blank" href="http://peter.michaux.ca/articles/our-backwards-dom-event-libraries" target="_blank">老外的文章</a>，文章提到了<strong>addEventListener</strong>第二个参数可以传入object，并且对象中handleEvent为事件处理函数。例如下面的javascript代码：</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>
	handleEvent<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;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body clicked'</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>
document.<span style="color: #660066;">body</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span>obj<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></pre></div></div>

<p>测试发现这段代码在IE9+，chrome，FF，opera等浏览器是正确的（测试范围可能不够宽），而且可以成功的绑定click事件。在Qwrap群里一问才知道是<a target="_blank" href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventListener" target="_blank">DOM2中接口</a>，很有意思的一个发现，<a target="_blank" href="http://weibo.com/itapir" target="_blank">@貘大大</a> 也发微博说自己先入为主了，我相信很多人跟<a target="_blank" href="http://weibo.com/itapir" target="_blank">@貘大大</a> 一样都先入为主了。</p>
<h2>addEventListener的handleEvent应用</h2>
<p>发现了这个“新大陆”有什么用法呢？哈哈~首先想到的是<a href="http://js8.in/736.html" target="_blank">对象引用</a>。看下面的javascript代码：</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
var obj = {
handleEvent: function() {
	alert('body clicked');
}
};
document.body.addEventListener('click',obj,false);
function fn2(){
	obj.handleEvent = function(){
		alert('fn2');
	};
}
&lt;/script&gt;
&lt;button onclick=&quot;fn2();&quot;&gt;change&lt;/button&gt;</pre></div></div>

<p>因为对象的引用关系，点击下button就可以切换绑定事件的处理函数，而不需要remove之前的事件，然后重新绑定一个新的处理函数。<br />
<span id="more-983"></span><br />
如果你觉得这个不够炫，那么下面的javascript代码可能就更有吸引力了哦~</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	addEvent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		el.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</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: #339933;">,</span>
	handleEvent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">target</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>
obj2.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>addEventListener handleEvent缺点</h2>
<p>显而易见，IE6~IE8不能使用……可是如果是高级浏览器还是不错的功能哦~</p>
<h3>扩展阅读</h3>
<p>http://peter.michaux.ca/articles/our-backwards-dom-event-libraries<br />
http://ajaxian.com/archives/an-alternative-way-to-addeventlistener
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/983.html" title="addEventListener第二个参数的handleEvent">http://js8.in/983.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/764.html" title="nodejs教程：配置nodejs.exe的windows目录结构" rel="bookmark inlinks">nodejs教程：配置nodejs.exe的windows目录结构</a></li>
<li><a href="http://js8.in/498.html" title="jQuery实现拖拽功能" rel="bookmark inlinks">jQuery实现拖拽功能</a></li>
<li><a href="http://js8.in/15.html" title="奋斗了两天！" rel="bookmark inlinks">奋斗了两天！</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/557.html" title="解决IE6背景图片不缓存的BUG" rel="bookmark inlinks">解决IE6背景图片不缓存的BUG</a></li>
<li><a href="http://js8.in/683.html" title="一道js正则笔试题" rel="bookmark inlinks">一道js正则笔试题</a></li>
<li><a href="http://js8.in/516.html" title="js8.in PR值升为4" rel="bookmark inlinks">js8.in PR值升为4</a></li>
<li><a href="http://js8.in/526.html" title="Ubuntu安装Samba实现跟windows文件共享" rel="bookmark inlinks">Ubuntu安装Samba实现跟windows文件共享</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2012. |
<a href="http://js8.in/983.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/addeventlistener" rel="tag">addEventListener</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?url=http%3A%2F%2Fjs8.in%2F480.html&from=http%3A%2F%2Fjs8.in%2F983.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?url=http%3A%2F%2Fjs8.in%2F473.html&from=http%3A%2F%2Fjs8.in%2F983.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?url=http%3A%2F%2Fjs8.in%2F529.html&from=http%3A%2F%2Fjs8.in%2F983.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?url=http%3A%2F%2Fjs8.in%2F408.html&from=http%3A%2F%2Fjs8.in%2F983.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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/983.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>webSlide — PPT演示文档javascript框架</title>
		<link>http://js8.in/974.html</link>
		<comments>http://js8.in/974.html#comments</comments>
		<pubDate>Mon, 27 Feb 2012 03:15:58 +0000</pubDate>
		<dc:creator>三水清</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[网络技术]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PPT]]></category>
		<category><![CDATA[webSlide]]></category>

		<guid isPermaLink="false">http://js8.in/?p=974</guid>
		<description><![CDATA[互联网公司内部难免会有些技术分享之类的活动，需要用到PPT，前端开发工程师都喜欢自己的PPT帅一点，当然我也一样。于是开始用impress.js做PPT，所以有了上一篇PPT《XSS注入和防范》。不过在用的过程中发现impress.js性能有问题，假如页面太多，或者图片太多，就会出现卡的现象。于是开始琢磨自己写个javascript演示文档框架。嗯，于是webSlide就诞生了！
webSlide是用于在线PPT演示文档的javascript框架
鸣谢
webSlide的想法来自于impress.js，但是本人在使用过程中发现impress.js经常卡，尤其是当演示文档页面和图片比较多之时，经常出现动画效果不流畅。
写webSlide代码过程中，研究过impress.js和reveal.js，向两位作者表示感谢。
演示页面
演示页面地址http://ksky521.github.com/webSlide/，由于页面比较多，所以需要一定的加载时间。
国内可以访问SAE上的演示页面：http://qdemo.sinaapp.com/ppt/udc.htm。
演示页面内容为给公司UDC部门同事进行javascript入门培训的内容~
说明
因为是PPT演示文档，所以需要投影仪分辨率，所以演示页面的最佳分辨率为全屏模式下的 1024*768 ，如果在自己电脑上查看，可以通过 ctrl + - 和 ctrl + + 缩放到合适的比例查看效果。
建议浏览器chrome 16+，全屏模式（F11），以达到最佳动画效果。在Firefox下会出现拖尾现象，个人认为是Firefox 8下对CSS3动画效果渲染太慢导致，其他浏览器未测试。

使用示例
wSlide({
    slideId:'slide',//演示文档id
    canvasId:'myCanvas',//画板id
    ctrlId:'slideCtrl'//控制部分id
});

参数说明


slideId: 演示文档内容部分ID，class为step为每页，slide为带边框的页面，必填
canvasId: canvas元素画板部分ID，选填
ctrlId: 控制部分ID，class为home代表返回首页，class为paint开启画板，class为clearIt清除画板，选填
presentClass: 当前幻灯片class，选填，默认为present
pastClass: 上一页幻灯片class，选填，默认为past
futureClass: 下一页幻灯片class，选填，默认为future


快捷键

空格/→/↓/Tab/pageDown：下一页
←/↑/pageUp：上一页
P：开画板
C：清除画板

版本库地址
支持三种访问协议：

HTTP协议： https://ksky521@github.com/ksky521/webSlide.git 。
Git协议： git://github.com/ksky521/webSlide.git 。
SSH协议： ssh://git@github.com:ksky521/webSlide.git 。

克隆版本库
操作示例：
$ git clone git://github.com/ksky521/webSlide.git

联系方式
作者博客：js8.in
作者新浪微博：@三水清

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

基于express+socket.io的nodejs聊天室
新浪微博应用：微博送大礼
校内人人网忽悠用户-陈一舟牛啊~
png在IE6下透明的几种方法
提供一个免费支持Jabber, AIM, MSN, Yahoo, Gtalk,Twitter 机器人
十枚腾讯微博内测邀请的种子
Javascript中模拟PHP的base64_decode和base64_encode函数
两个简单的jQuery幻灯片实例


© 三水清 for 三水清, 2012. &#124;
6 comments &#124;在微博关注我：@清-三水清
收藏到：
人人网 &#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入门到高级PPT" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F750.html&from=http%3A%2F%2Fjs8.in%2F974.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/10979311.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入门到高级PPT</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="nodePPT — nodejs做的手机端控制PC端的PPT演示文档javascript框架" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F989.html&from=http%3A%2F%2Fjs8.in%2F974.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/04/21/21201860.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;">nodePPT — nodejs做的手机端控制PC端的PPT演示文档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?url=http%3A%2F%2Fjs8.in%2F467.html&from=http%3A%2F%2Fjs8.in%2F974.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>
                <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?url=http%3A%2F%2Fjs8.in%2F466.html&from=http%3A%2F%2Fjs8.in%2F974.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" 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>互联网公司内部难免会有些技术分享之类的活动，需要用到PPT，<strong>前端开发</strong>工程师都喜欢自己的PPT帅一点，当然我也一样。于是开始用impress.js做PPT，所以有了上一篇PPT《<a target="_blank" href="http://js8.in/951.html" target="_blank">XSS注入和防范</a>》。不过在用的过程中发现impress.js性能有问题，假如页面太多，或者图片太多，就会出现卡的现象。于是开始琢磨自己写个javascript演示文档框架。嗯，于是<a href="https://github.com/ksky521/webSlide" target="_blank">webSlide</a>就诞生了！</p>
<p><a target="_blank" style="border-image: initial; font: inherit; color: #4183c4; text-decoration: none; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; margin: 0px; border: 0px initial initial;" href="https://github.com/ksky521/webSlide">webSlide</a><span style="color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 14px; line-height: 22px;">是用于在线PPT演示文档的javascript框架</span></p>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">鸣谢</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">webSlide的想法来自于impress.js，但是本人在使用过程中发现impress.js经常卡，尤其是当演示文档页面和图片比较多之时，经常出现动画效果不流畅。</p>
<p style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">写webSlide代码过程中，研究过<a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://bartaz.github.com/impress.js">impress.js</a>和<a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://lab.hakim.se/reveal-js/">reveal.js</a>，向两位作者表示感谢。</p>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">演示页面</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">演示页面地址<a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://ksky521.github.com/webSlide/"></a><a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://ksky521.github.com/webSlide/">http://ksky521.github.com/webSlide/</a>，由于页面比较多，所以需要一定的加载时间。</p>
<p style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">国内可以访问SAE上的演示页面：<a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://qdemo.sinaapp.com/ppt/udc.htm"></a><a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://qdemo.sinaapp.com/ppt/udc.htm">http://qdemo.sinaapp.com/ppt/udc.htm</a>。</p>
<p style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">演示页面内容为给公司UDC部门同事进行javascript入门培训的内容~</p>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">说明</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">因为是PPT演示文档，所以需要投影仪分辨率，所以演示页面的最佳分辨率为全屏模式下的 <code style="margin-top: 0px; margin-right: 2px; margin-bottom: 0px; margin-left: 2px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; white-space: nowrap; background-color: #f8f8f8; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid #eaeaea;">1024*768</code> ，如果在自己电脑上查看，可以通过 <code style="margin-top: 0px; margin-right: 2px; margin-bottom: 0px; margin-left: 2px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; white-space: nowrap; background-color: #f8f8f8; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid #eaeaea;">ctrl + -</code> 和 <code style="margin-top: 0px; margin-right: 2px; margin-bottom: 0px; margin-left: 2px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; white-space: nowrap; background-color: #f8f8f8; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid #eaeaea;">ctrl + +</code> 缩放到合适的比例查看效果。</p>
<p style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">建议浏览器chrome 16+，全屏模式（F11），以达到最佳动画效果。在Firefox下会出现拖尾现象，个人认为是Firefox 8下对CSS3动画效果渲染太慢导致，其他浏览器未测试。</p>
<p><span id="more-974"></span></p>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">使用示例</h2>
<pre style="margin-top: 15px; margin-bottom: 15px; padding-top: 6px; padding-right: 10px; padding-bottom: 6px; padding-left: 10px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: 19px; overflow-x: auto; overflow-y: auto; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: #333333; border: 1px solid #cccccc;"><code style="border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; padding: 0px; margin: 0px; border: initial none initial;">wSlide({
    slideId:'slide',//演示文档id
    canvasId:'myCanvas',//画板id
    ctrlId:'slideCtrl'//控制部分id
});
</code></pre>
<h4 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px; border: 0px initial initial;">参数说明</h4>
<blockquote style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 0px; padding-right: 15px; padding-bottom: 0px; padding-left: 15px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 4px; border-style: initial; border-color: initial; border-image: initial; font: inherit; quotes: none; border-left-style: solid; border-left-color: #dddddd; color: #777777; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px;">
<ul style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 30px; border-image: initial; font-size: 14px; font: inherit; margin: 0px; border: 0px initial initial;">
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">slideId: 演示文档内容部分ID，class为step为每页，slide为带边框的页面，<strong style="border-image: initial; font-size: 14px; font: inherit; font-weight: bold; padding: 0px; margin: 0px; border: 0px initial initial;">必填</strong></li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">canvasId: canvas元素画板部分ID，<strong style="border-image: initial; font-size: 14px; font: inherit; font-weight: bold; padding: 0px; margin: 0px; border: 0px initial initial;">选填</strong></li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">ctrlId: 控制部分ID，class为home代表返回首页，class为paint开启画板，class为clearIt清除画板，<strong style="border-image: initial; font-size: 14px; font: inherit; font-weight: bold; padding: 0px; margin: 0px; border: 0px initial initial;">选填</strong></li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">presentClass: 当前幻灯片class，<strong style="border-image: initial; font-size: 14px; font: inherit; font-weight: bold; padding: 0px; margin: 0px; border: 0px initial initial;">选填，默认为present</strong></li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">pastClass: 上一页幻灯片class，<strong style="border-image: initial; font-size: 14px; font: inherit; font-weight: bold; padding: 0px; margin: 0px; border: 0px initial initial;">选填，默认为past</strong></li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">futureClass: 下一页幻灯片class，<strong style="border-image: initial; font-size: 14px; font: inherit; font-weight: bold; padding: 0px; margin: 0px; border: 0px initial initial;">选填，默认为future</strong></li>
</ul>
</blockquote>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">快捷键</h2>
<ul style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 30px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; border: 0px initial initial;">
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">空格/→/↓/Tab/pageDown：下一页</li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">←/↑/pageUp：上一页</li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">P：开画板</li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">C：清除画板</li>
</ul>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">版本库地址</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">支持三种访问协议：</p>
<ul style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 30px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; border: 0px initial initial;">
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">HTTP协议： <code style="margin-top: 0px; margin-right: 2px; margin-bottom: 0px; margin-left: 2px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; white-space: nowrap; background-color: #f8f8f8; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid #eaeaea;">https://ksky521@github.com/ksky521/webSlide.git</code> 。</li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">Git协议： <code style="margin-top: 0px; margin-right: 2px; margin-bottom: 0px; margin-left: 2px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; white-space: nowrap; background-color: #f8f8f8; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid #eaeaea;">git://github.com/ksky521/webSlide.git</code> 。</li>
<li style="margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font-size: 14px; font: inherit; padding: 0px; border: 0px initial initial;">SSH协议： <code style="margin-top: 0px; margin-right: 2px; margin-bottom: 0px; margin-left: 2px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 5px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; white-space: nowrap; background-color: #f8f8f8; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid #eaeaea;">ssh://git@github.com:ksky521/webSlide.git</code> 。</li>
</ul>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">克隆版本库</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">操作示例：</p>
<pre style="margin-top: 15px; margin-bottom: 15px; padding-top: 6px; padding-right: 10px; padding-bottom: 6px; padding-left: 10px; border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: 19px; overflow-x: auto; overflow-y: auto; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: #333333; border: 1px solid #cccccc;"><code style="border-image: initial; font: inherit; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; line-height: normal; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; padding: 0px; margin: 0px; border: initial none initial;">$ git clone git://github.com/ksky521/webSlide.git
</code></pre>
<h2 style="margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; font: inherit; -webkit-font-smoothing: antialiased; border-bottom-style: solid; border-bottom-color: #cccccc; font-family: Helvetica, arial, freesans, clean, sans-serif; padding: 0px;">联系方式</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">作者博客：<a style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://js8.in/">js8.in</a></p>
<p style="margin-top: 15px; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; border-image: initial; font: inherit; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; line-height: 22px; padding: 0px; border: 0px initial initial;">作者新浪微博：<a target="_blank" style="border-image: initial; font-size: 14px; font: inherit; color: #4183c4; text-decoration: none; padding: 0px; margin: 0px; border: 0px initial initial;" href="http://weibo.com/sanshuiqing">@三水清</a></p>
<div class="copyright">
<strong>声明：</strong>文章未声明为原创文章，本文链接 <a href="http://js8.in/974.html" title="webSlide — PPT演示文档javascript框架">http://js8.in/974.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/272.html" title="普通话考试！~" rel="bookmark inlinks">普通话考试！~</a></li>
<li><a href="http://js8.in/646.html" title="分享一个php验证码类，支持中文字母数字，还可自定义字体" rel="bookmark inlinks">分享一个php验证码类，支持中文字母数字，还可自定义字体</a></li>
<li><a href="http://js8.in/52.html" title="万众期待的Ajax评论发布啦-dedecms扩展工具" rel="bookmark inlinks">万众期待的Ajax评论发布啦-dedecms扩展工具</a></li>
<li><a href="http://js8.in/287.html" title="自己写的DeDeCMS v5.3三级菜单" rel="bookmark inlinks">自己写的DeDeCMS v5.3三级菜单</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/391.html" title="Google2009年Logo大盘点——第一季" rel="bookmark inlinks">Google2009年Logo大盘点——第一季</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/706.html" title="IE6中a标签location.href失效解决方法" rel="bookmark inlinks">IE6中a标签location.href失效解决方法</a></li>
</ul>
<hr />
<p><small>© 三水清 for <a href="http://js8.in">三水清</a>, 2012. |
<a href="http://js8.in/974.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/ppt" rel="tag">PPT</a>, <a href="http://js8.in/tag/webslide" rel="tag">webSlide</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入门到高级PPT" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F750.html&from=http%3A%2F%2Fjs8.in%2F974.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/10979311.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入门到高级PPT</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="nodePPT — nodejs做的手机端控制PC端的PPT演示文档javascript框架" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F989.html&from=http%3A%2F%2Fjs8.in%2F974.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/04/21/21201860.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;">nodePPT — nodejs做的手机端控制PC端的PPT演示文档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?url=http%3A%2F%2Fjs8.in%2F467.html&from=http%3A%2F%2Fjs8.in%2F974.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>
                <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?url=http%3A%2F%2Fjs8.in%2F466.html&from=http%3A%2F%2Fjs8.in%2F974.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" 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/974.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<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?url=http%3A%2F%2Fjs8.in%2F480.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/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?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?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?url=http%3A%2F%2Fjs8.in%2F408.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/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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/7.html" title="Cause I Believe" rel="bookmark inlinks">Cause I Believe</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/416.html" title="jQuery选择器使用详解" rel="bookmark inlinks">jQuery选择器使用详解</a></li>
<li><a href="http://js8.in/20.html" title="手把手图解教你使用PQ魔法师调节硬盘分区大小" rel="bookmark inlinks">手把手图解教你使用PQ魔法师调节硬盘分区大小</a></li>
<li><a href="http://js8.in/338.html" title="最新的DreamHost优惠码免费发放" rel="bookmark inlinks">最新的DreamHost优惠码免费发放</a></li>
<li><a href="http://js8.in/397.html" title="Opera Chrome Safari浏览器的CSS Hack方法" rel="bookmark inlinks">Opera Chrome Safari浏览器的CSS Hack方法</a></li>
<li><a href="http://js8.in/764.html" title="nodejs教程：配置nodejs.exe的windows目录结构" rel="bookmark inlinks">nodejs教程：配置nodejs.exe的windows目录结构</a></li>
<li><a href="http://js8.in/589.html" title="迅雷7起航版一键破解--让非会员也可免费体验" rel="bookmark inlinks">迅雷7起航版一键破解&#8211;让非会员也可免费体验</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?url=http%3A%2F%2Fjs8.in%2F480.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/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?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?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?url=http%3A%2F%2Fjs8.in%2F408.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/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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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 ™
随机文章

php获取来访者IP信息
真正的jQuery的radio与checkbox取值
WinHttp.WinHttpRequest.5.1—可以用作抓取的msxml 4.0底层对象
chrome扩展应用开发教程之调试和打包上线
Sql语句优化注意
javascript事件：获取事件对象getEvent函数
普通话考试！~
发布一个Follow5的API PHP类


© 三水清 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?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>
                <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?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?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?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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/698.html" title="javascript继承的写法" rel="bookmark inlinks">javascript继承的写法</a></li>
<li><a href="http://js8.in/558.html" title="onbeforeunload与a标签在IE中的冲突bug" rel="bookmark inlinks">onbeforeunload与a标签在IE中的冲突bug</a></li>
<li><a href="http://js8.in/518.html" title="使用Apache htpasswd.exe来管理SVN帐户" rel="bookmark inlinks">使用Apache htpasswd.exe来管理SVN帐户</a></li>
<li><a href="http://js8.in/351.html" title="使用CSS控制打印页面格式" rel="bookmark inlinks">使用CSS控制打印页面格式</a></li>
<li><a href="http://js8.in/283.html" title="jQuery+google weather API轻松实现天气地图（二）" rel="bookmark inlinks">jQuery+google weather API轻松实现天气地图（二）</a></li>
<li><a href="http://js8.in/287.html" title="自己写的DeDeCMS v5.3三级菜单" rel="bookmark inlinks">自己写的DeDeCMS v5.3三级菜单</a></li>
<li><a href="http://js8.in/640.html" title="dedeeims列表页面导航条当前样式实现方法" rel="bookmark inlinks">dedeeims列表页面导航条当前样式实现方法</a></li>
<li><a href="http://js8.in/589.html" title="迅雷7起航版一键破解--让非会员也可免费体验" rel="bookmark inlinks">迅雷7起航版一键破解&#8211;让非会员也可免费体验</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?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>
                <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?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?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?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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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?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?url=http%3A%2F%2Fjs8.in%2F473.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/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?url=http%3A%2F%2Fjs8.in%2F467.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/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>
                <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?url=http%3A%2F%2Fjs8.in%2F529.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/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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/461.html" title="断桥残雪部落格更换新域名JS8.in" rel="bookmark inlinks">断桥残雪部落格更换新域名JS8.in</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/359.html" title="提前体验Google新页面" rel="bookmark inlinks">提前体验Google新页面</a></li>
<li><a href="http://js8.in/527.html" title="断桥残雪爱墙IE发布祝福bug更新" rel="bookmark inlinks">断桥残雪爱墙IE发布祝福bug更新</a></li>
<li><a href="http://js8.in/620.html" title="关于wBox iFrame回调关闭问题的解决方案" rel="bookmark inlinks">关于wBox iFrame回调关闭问题的解决方案</a></li>
<li><a href="http://js8.in/564.html" title="Google Map API教程之使用GControl对象在地图上添加control按钮" rel="bookmark inlinks">Google Map API教程之使用GControl对象在地图上添加control按钮</a></li>
<li><a href="http://js8.in/784.html" title="使用socket.io和node.js搭建websocket应用" rel="bookmark inlinks">使用socket.io和node.js搭建websocket应用</a></li>
<li><a href="http://js8.in/379.html" title="cURL常用的几个PHP函数" rel="bookmark inlinks">cURL常用的几个PHP函数</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?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?url=http%3A%2F%2Fjs8.in%2F473.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/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?url=http%3A%2F%2Fjs8.in%2F467.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/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>
                <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?url=http%3A%2F%2Fjs8.in%2F529.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/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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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 ™
随机文章

委员们又闹笑话了
Wordpress Follow5 插件开发日记—插件后台Ajax应用及jQuery的使用
jQuery插件&#8211;wBox发布
CSS 3.0 参考手册 (中文版)下载
08.11.14
javascript匿名函数
jQuery浏览器判断一个bug，以及修改建议
送给你的TA另类的礼物，定要TA感动——爱墙


© 三水清 for 三水清, 2011. &#124;
8 comments &#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="IE6下javascript不能隐藏select标签的bug" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?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 PNG图片不透明问题的解决方法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="png在IE6下透明的几种方法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F448.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/23/8885105.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;">png在IE6下透明的几种方法</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?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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/48.html" title="Ajax评论bug更新" rel="bookmark inlinks">Ajax评论bug更新</a></li>
<li><a href="http://js8.in/528.html" title="悼念地震，网站变灰CSS代码" rel="bookmark inlinks">悼念地震，网站变灰CSS代码</a></li>
<li><a href="http://js8.in/60.html" title="断桥残雪爱墙系统上线" rel="bookmark inlinks">断桥残雪爱墙系统上线</a></li>
<li><a href="http://js8.in/385.html" title="六个精美的WordPress圣诞插件" rel="bookmark inlinks">六个精美的WordPress圣诞插件</a></li>
<li><a href="http://js8.in/44.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/559.html" title="实时公布腾讯微博邀请种子" rel="bookmark inlinks">实时公布腾讯微博邀请种子</a></li>
<li><a href="http://js8.in/517.html" title="快毕业了，老师站在讲台上：“探讨三个问题。”" rel="bookmark inlinks">快毕业了，老师站在讲台上：“探讨三个问题。”</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下javascript不能隐藏select标签的bug" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?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 PNG图片不透明问题的解决方法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="png在IE6下透明的几种方法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F448.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/23/8885105.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;">png在IE6下透明的几种方法</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?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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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 ™
随机文章

再来几个腾讯微博的邀请种子
webSlide — PPT演示文档javascript框架
基于express+socket.io的nodejs聊天室
Ubuntu安装Samba实现跟windows文件共享
linux安装PHP加速器eAccelerator
[转]利用跨域资源共享（CORS）实现ajax跨域调用
Opera Chrome Safari浏览器的CSS Hack方法
博客终于再次开通了


© 三水清 for 三水清, 2011. &#124;
2 comments &#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?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?url=http%3A%2F%2Fjs8.in%2F809.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/10989154.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?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?url=http%3A%2F%2Fjs8.in%2F467.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/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" 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/644.html" title="利用google博客的ping服务接口API加快网站收录" rel="bookmark inlinks">利用google博客的ping服务接口API加快网站收录</a></li>
<li><a href="http://js8.in/640.html" title="dedeeims列表页面导航条当前样式实现方法" rel="bookmark inlinks">dedeeims列表页面导航条当前样式实现方法</a></li>
<li><a href="http://js8.in/689.html" title="NodeList集合跟Array数组的区别" rel="bookmark inlinks">NodeList集合跟Array数组的区别</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/606.html" title="解决IE6不支持max-height的bug" rel="bookmark inlinks">解决IE6不支持max-height的bug</a></li>
<li><a href="http://js8.in/351.html" title="使用CSS控制打印页面格式" rel="bookmark inlinks">使用CSS控制打印页面格式</a></li>
<li><a href="http://js8.in/6.html" title="经典QQ签名" rel="bookmark inlinks">经典QQ签名</a></li>
<li><a href="http://js8.in/638.html" title="再谈Iframe的问题" rel="bookmark inlinks">再谈Iframe的问题</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?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?url=http%3A%2F%2Fjs8.in%2F809.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/10989154.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?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?url=http%3A%2F%2Fjs8.in%2F467.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/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" 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?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?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?url=http%3A%2F%2Fjs8.in%2F408.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/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="[转]利用跨域资源共享（CORS）实现ajax跨域调用" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F685.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/09/21/7214143.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;">[转]利用跨域资源共享（CORS）实现ajax跨域调用</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/352.html" title="wordpress Follow5插件发布" rel="bookmark inlinks">wordpress Follow5插件发布</a></li>
<li><a href="http://js8.in/60.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/683.html" title="一道js正则笔试题" rel="bookmark inlinks">一道js正则笔试题</a></li>
<li><a href="http://js8.in/605.html" title="javascript简单实现checkbox的全选与反选" rel="bookmark inlinks">javascript简单实现checkbox的全选与反选</a></li>
<li><a href="http://js8.in/401.html" title="微软官方发布IE调试工具：SuperPreview for IE" rel="bookmark inlinks">微软官方发布IE调试工具：SuperPreview for IE</a></li>
<li><a href="http://js8.in/502.html" title="jQuery插件--wBox发布" rel="bookmark inlinks">jQuery插件&#8211;wBox发布</a></li>
<li><a href="http://js8.in/267.html" title="新功能-QQ秀上线" rel="bookmark inlinks">新功能-QQ秀上线</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?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?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?url=http%3A%2F%2Fjs8.in%2F408.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/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="[转]利用跨域资源共享（CORS）实现ajax跨域调用" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F685.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/09/21/7214143.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;">[转]利用跨域资源共享（CORS）实现ajax跨域调用</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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?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?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?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事件：获取事件对象getEvent函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F703.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/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" 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/469.html" title="jQuery 1.4.1 中文参考文档下载" rel="bookmark inlinks">jQuery 1.4.1 中文参考文档下载</a></li>
<li><a href="http://js8.in/670.html" title="提高网站访问速度的十个技巧" rel="bookmark inlinks">提高网站访问速度的十个技巧</a></li>
<li><a href="http://js8.in/330.html" title="光良,第一次—珍喜欢" rel="bookmark inlinks">光良,第一次—珍喜欢</a></li>
<li><a href="http://js8.in/479.html" title="CSS+jQuery实现滑动幻灯片实例详解" rel="bookmark inlinks">CSS+jQuery实现滑动幻灯片实例详解</a></li>
<li><a href="http://js8.in/941.html" title="PHP实现javascript的escape和unescape函数" rel="bookmark inlinks">PHP实现javascript的escape和unescape函数</a></li>
<li><a href="http://js8.in/569.html" title="IE中多帧GIF可以触发多次Image().load事件" rel="bookmark inlinks">IE中多帧GIF可以触发多次Image().load事件</a></li>
<li><a href="http://js8.in/591.html" title="切换订阅地址到feedsky" rel="bookmark inlinks">切换订阅地址到feedsky</a></li>
<li><a href="http://js8.in/646.html" title="分享一个php验证码类，支持中文字母数字，还可自定义字体" rel="bookmark inlinks">分享一个php验证码类，支持中文字母数字，还可自定义字体</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/851.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/%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?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?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?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事件：获取事件对象getEvent函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fjs8.in%2F703.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/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" 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>3</slash:comments>
		</item>
	</channel>
</rss>

