<?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>简单生活 —— Kevin Yang的博客 &#187; 空值</title> <atom:link href="http://www.imkevinyang.com/tags/%e7%a9%ba%e5%80%bc/feed" rel="self" type="application/rss+xml" /><link>http://www.imkevinyang.com</link> <description>It&#039;s all about sharing</description> <lastBuildDate>Mon, 06 Sep 2010 08:00:00 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Javascript 中的false,零值,null,undefined和空字符串对象</title><link>http://www.imkevinyang.com/2009/07/javascript-%e4%b8%ad%e7%9a%84false%e9%9b%b6%e5%80%bcnullundefined%e5%92%8c%e7%a9%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%af%b9%e8%b1%a1.html</link> <comments>http://www.imkevinyang.com/2009/07/javascript-%e4%b8%ad%e7%9a%84false%e9%9b%b6%e5%80%bcnullundefined%e5%92%8c%e7%a9%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%af%b9%e8%b1%a1.html#comments</comments> <pubDate>Tue, 07 Jul 2009 12:08:00 +0000</pubDate> <dc:creator>Kevin Yang</dc:creator> <category><![CDATA[Web传统技术]]></category> <category><![CDATA[技术随笔]]></category> <category><![CDATA[false]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[null]]></category> <category><![CDATA[undefined]]></category> <category><![CDATA[假值]]></category> <category><![CDATA[空值]]></category> <category><![CDATA[空字符串]]></category> <category><![CDATA[零值]]></category><guid isPermaLink="false">http://www.imkevinyang.com/2009/07/javascript-%e4%b8%ad%e7%9a%84false%e3%80%810%e3%80%81null%e3%80%81undefined%e5%92%8c%e7%a9%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%af%b9%e8%b1%a1.html</guid> <description><![CDATA[<p>在Javascript中，我们经常会接触到题目中提到的这5个比较特别的对象--false、0、空字符串、null和undefined。这几个对象很容易用错，因此在使用时必须得小心。</p><h2>类型检测</h2><p>我们下来看看他们的类型分别是什么：</p><pre class="csharpcode">
<span class="kwrd">&#60;</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&#62;</span>
    alert(<span class="kwrd">typeof</span>(<span class="kwrd">false</span>) === <span class="str">'boolean'</span>);
    alert(<span class="kwrd">typeof</span>(0) === <span class="str">'number'</span>);
    alert(<span class="kwrd">typeof&#8230;</span></pre>]]></description> <content:encoded><![CDATA[<p>在Javascript中，我们经常会接触到题目中提到的这5个比较特别的对象--false、0、空字符串、null和undefined。这几个对象很容易用错，因此在使用时必须得小心。</p><h2>类型检测</h2><p>我们下来看看他们的类型分别是什么：</p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    alert(<span class="kwrd">typeof</span>(<span class="kwrd">false</span>) === <span class="str">'boolean'</span>);
    alert(<span class="kwrd">typeof</span>(0) === <span class="str">'number'</span>);
    alert(<span class="kwrd">typeof</span>(<span class="str">""</span>) === <span class="str">'string'</span>);
    alert(<span class="kwrd">typeof</span>(<span class="kwrd">null</span>) === <span class="str">'object'</span>);
    alert(<span class="kwrd">typeof</span> undefined === <span class="str">'undefined'</span>);
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><p>运行上述代码，弹出的对话框应该显示的都是true。也就是说，false是布尔类型对象，0是数字类型对象，空字符串是字符串类型对象，null是object对象，undefined类型还是undefined。</p><h2>互等性</h2><p>当你用==操作符将false对象和其他对象进行比较的时候，你会发现， <strong><span style="COLOR: #008000">只有0和空字符串等于false；undefined和null对象并不等于false对象，而null和undefined是相等的</span></strong></p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span><span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    alert(<span class="kwrd">false</span> == undefined);
    alert(<span class="kwrd">false</span> == <span class="kwrd">null</span>);
    alert(<span class="kwrd">false</span> == 0);
    alert(<span class="kwrd">false</span> == <span class="str">""</span>);
    alert(<span class="kwrd">null</span> == undefined);
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><p>我们可以把0、空字符串和false归为一类，称为"假值"；把null和undefined归为一类，称为"空值"。假值还算一个有效的对象，因此可以对其使用toString等类型相关的方法，而空值则不行。下面的代码将会抛出异常：</p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span><span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    alert(<span class="kwrd">false</span>.toString());    <span class="rem">// "false"</span>
    alert(<span class="str">""</span>.charAt(0));        <span class="rem">// ""</span>
    alert((0).toExponential(10));  <span class="rem">// 0.0000000e+0</span>
    alert(undefined.toString());    <span class="rem">// throw exception "undefined has no properties"</span>
    alert(<span class="kwrd">null</span>.toString());             <span class="rem">// "null has no properties"</span>
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><h2>字符串表示</h2><p>虽然空值不能调用toString方法，但是却可以使用String构造函数进行构造。 <strong><span style="COLOR: #008000">像decodeURI这样的函数，如果传入的是undefined或者null，返回的是"undefined"和"null"字符串</span></strong> 。这点很容易用错。</p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    alert(String(<span class="kwrd">false</span>));    <span class="rem">// "false"</span>
    alert(String(<span class="str">""</span>));        <span class="rem">// ""</span>
    alert(String(0));  <span class="rem">// 0.0000000e+0</span>
    alert(String(undefined));    <span class="rem">// "undefined"</span>
    alert(String(<span class="kwrd">null</span>));             <span class="rem">// "null"</span>

    alert(decodeURI(undefined));<span class="rem">// "undefined"</span>
    alert(decodeURI(<span class="kwrd">null</span>));<span class="rem">// "null"</span>
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><h2>假值和空值作为if条件分支</h2><p>假值和空值有一个共性，那就是在 <strong><span style="COLOR: #008000"><strong><span style="COLOR: #008000">作为if的条件分支时，均被视为false</span></strong> ；应用"!"操作之后得到的均为true</span></strong> 。如下示例代码：</p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span><span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">var</span> ar = [undefined,<span class="kwrd">false</span>,0,<span class="str">""</span>,<span class="kwrd">null</span>];
    <span class="kwrd">for</span>(<span class="kwrd">var</span> i = 0,len = ar.length; i &lt; len; i++){
        <span class="kwrd">if</span>(ar[i]){
            alert(<span class="str">"你不应该看到此对话框!"</span>);
        }
    }
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><p>这是因为，这几个对象均被视为各自类型中的无效值或空值。因此if分支中这些对象均被视为false对待。</p><h2>null和undefined的区别</h2><p>这两个空值的区别也是容易混淆的。</p><p><strong><span style="COLOR: #008000">undefined和null对象无非是两个特殊对象，undefined表示无效对象，null表示空对象。如果变量显式或者隐式（由Javascript引擎进行赋值）地被赋予了undefined，那么代表了此变量未被定义，如果被赋予null值，则代表此变量被初始化为空值。</span></strong></p><p>在Javascript中，变量是通过var声明，=赋值符进行定义（初始化变量所指向的对象）。当然，如果声明一个全局变量（作为window属性）可以不使用var关键字。变量可以在声明的同时进行定义。</p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span><span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">var</span> undefinedVariable,nullVariable = <span class="kwrd">null</span>;
    alert(undefinedVariable); <span class="rem">// "undefined"</span>
    alert(window.undefinedVariable);        <span class="rem">// "undefined"</span>
    alert(window.abcd);        <span class="rem">// "undefined"</span>
    alert(nullVariable);          <span class="rem">// "null"</span>
    alert(abcd);                    <span class="rem">// throw exception "abcd is not defined"</span>
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><p>其实， <strong><span style="COLOR: #008000">变量如果声明了但是没有初始化，那么Javascript引擎会将此变量自动指向undefined对象。</span></strong></p><p>这里需要注意，我们在上面引用window.abcd时，弹出的是undefined；而直接引用abcd变量时，却抛出了一个异常。这是由于Javascript引擎对于没有显式指定对象链的变量，会尝试从最近的作用域开始查找变量，查找失败，则退到父级作用链进行查找。如果均查找失败，则抛出"变量未定义"的异常。</p><p>这两个值在进行数字运算的时候也有不同。</p><pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">script</span><span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">&gt;</span>
    alert(1+undefined);    <span class="rem">// "NaN"</span>
    alert(1+<span class="kwrd">null</span>);             <span class="rem">// "1"</span>
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>
</pre><p>从null和undefined的意义上来说，这是很好理解的。</p>标签：<a href="http://www.imkevinyang.com/tags/false" title="false" rel="tag">false</a>, <a href="http://www.imkevinyang.com/tags/javascript" title="Javascript" rel="tag">Javascript</a>, <a href="http://www.imkevinyang.com/tags/null" title="null" rel="tag">null</a>, <a href="http://www.imkevinyang.com/tags/undefined" title="undefined" rel="tag">undefined</a>, <a href="http://www.imkevinyang.com/categories/techarticles/web%e4%bc%a0%e7%bb%9f%e6%8a%80%e6%9c%af" title="Web传统技术" rel="tag">Web传统技术</a>, <a href="http://www.imkevinyang.com/tags/%e5%81%87%e5%80%bc" title="假值" rel="tag">假值</a>, <a href="http://www.imkevinyang.com/categories/techarticles" title="技术随笔" rel="tag">技术随笔</a>, <a href="http://www.imkevinyang.com/tags/%e7%a9%ba%e5%80%bc" title="空值" rel="tag">空值</a>, <a href="http://www.imkevinyang.com/tags/%e7%a9%ba%e5%ad%97%e7%ac%a6%e4%b8%b2" title="空字符串" rel="tag">空字符串</a>, <a href="http://www.imkevinyang.com/tags/%e9%9b%b6%e5%80%bc" title="零值" rel="tag">零值</a><br /><h4 style="background-color:#3B3B3B;border-bottom:2px groove gray;color:#F2F2F2;margin-top:20px;padding:6px 6px 6px 15px;margin:20px 0px 0px 0px">你可能对下面的文章感兴趣</h4><ul class="st-related-posts"><li><a href="http://www.imkevinyang.com/2009/11/button%e6%a0%87%e7%ad%be%e9%bc%a0%e6%a0%87%e7%82%b9%e5%87%bb%e4%ba%8b%e4%bb%b6%e7%9a%84%e8%a7%a6%e5%8f%91%e6%ba%90%e9%97%ae%e9%a2%98.html" title="Button标签鼠标点击事件的触发源问题 (2009/11/27)">Button标签鼠标点击事件的触发源问题</a> (2009/11/27)</li><li><a href="http://www.imkevinyang.com/2010/01/document-referrer%e4%b8%a2%e5%a4%b1%e7%9a%84%e5%87%a0%e4%b8%aa%e5%8e%9f%e5%9b%a0.html" title="Document.Referrer丢失的几个原因 (2010/01/18)">Document.Referrer丢失的几个原因</a> (2010/01/18)</li><li><a href="http://www.imkevinyang.com/2009/03/ie%e4%b8%ad%e4%bd%bf%e7%94%a8windowopen%e6%89%93%e5%bc%80%e6%96%b0%e7%aa%97%e5%8f%a3%e6%97%b6%e6%97%a0%e6%b3%95%e8%8e%b7%e5%8f%96referrer%e5%af%b9%e8%b1%a1.html" title="IE中使用window.open打开新窗口时无法获取Referrer对象 (2009/03/07)">IE中使用window.open打开新窗口时无法获取Referrer对象</a> (2009/03/07)</li><li><a href="http://www.imkevinyang.com/2010/07/javajs%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%8c%b9%e9%85%8d%e5%b5%8c%e5%a5%97html%e6%a0%87%e7%ad%be.html" title="Java/Js如何使用正则表达式匹配嵌套Html标签 (2010/07/30)">Java/Js如何使用正则表达式匹配嵌套Html标签</a> (2010/07/30)</li><li><a href="http://www.imkevinyang.com/2009/05/javascript%e4%b8%ad%e8%8e%b7%e5%8f%96%e5%87%ba%e9%94%99%e4%bb%a3%e7%a0%81%e6%89%80%e5%9c%a8%e6%96%87%e4%bb%b6%e5%8f%8a%e8%a1%8c%e6%95%b0.html" title="Javascript中获取出错代码所在文件及行数 (2009/05/18)">Javascript中获取出错代码所在文件及行数</a> (2009/05/18)</li><li><a href="http://www.imkevinyang.com/2009/04/javascript%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%93%88%e5%b8%8c%e5%87%bd%e6%95%b0.html" title="Javascript字符串哈希函数 (2009/04/11)">Javascript字符串哈希函数</a> (2009/04/11)</li><li><a href="http://www.imkevinyang.com/2009/06/javascript%e6%93%8d%e7%ba%b5cookie.html" title="Javascript操纵Cookie (2009/06/11)">Javascript操纵Cookie</a> (2009/06/11)</li><li><a href="http://www.imkevinyang.com/2010/05/%e4%b8%ba%e4%bb%80%e4%b9%88iis77-5%e7%9a%84gzip%e4%b8%8d%e8%b5%b7%e4%bd%9c%e7%94%a8.html" title="为什么IIS7/7.5的Gzip不起作用 (2010/05/08)">为什么IIS7/7.5的Gzip不起作用</a> (2010/05/08)</li><li><a href="http://www.imkevinyang.com/2009/09/%e4%bd%bf%e7%94%a8%e7%9b%b8%e5%af%b9url%e6%97%a0%e7%bc%9d%e5%88%87%e6%8d%a2http-https.html" title="使用相对Url无缝切换HTTP-HTTPS (2009/09/18)">使用相对Url无缝切换HTTP-HTTPS</a> (2009/09/18)</li><li><a href="http://www.imkevinyang.com/2010/07/%e5%87%a0%e4%b8%aa%e6%9c%89%e8%b6%a3%e7%9a%84javascript-hack.html" title="几个有趣的Javascript Hack (2010/07/23)">几个有趣的Javascript Hack</a> (2010/07/23)</li></ul>]]></content:encoded> <wfw:commentRss>http://www.imkevinyang.com/2009/07/javascript-%e4%b8%ad%e7%9a%84false%e9%9b%b6%e5%80%bcnullundefined%e5%92%8c%e7%a9%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%af%b9%e8%b1%a1.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>