??xml version="1.0" encoding="utf-8" standalone="yes"?>91精品久久久久久,国产精品久久久久久亚洲毛片,国产美女性感在线观看懂色avhttp://www.aygfsteel.com/smcdl/category/22723.html抽象xI,I即是抽象?/description>zh-cnMon, 19 Oct 2009 18:36:52 GMTMon, 19 Oct 2009 18:36:52 GMT60关于样式表对象style与currentStyle的区?/title><link>http://www.aygfsteel.com/smcdl/archive/2009/10/18/298782.html</link><dc:creator>teddy</dc:creator><author>teddy</author><pubDate>Sun, 18 Oct 2009 14:59:00 GMT</pubDate><guid>http://www.aygfsteel.com/smcdl/archive/2009/10/18/298782.html</guid><wfw:comment>http://www.aygfsteel.com/smcdl/comments/298782.html</wfw:comment><comments>http://www.aygfsteel.com/smcdl/archive/2009/10/18/298782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/smcdl/comments/commentRss/298782.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/smcdl/services/trackbacks/298782.html</trackback:ping><description><![CDATA[style和currentStyle都能用来取得当前样式表中的信息,但是style只能取得在HTML中用style声明的样式表信息Q无法取得通过class声明的样式表信息Q而currentStyle两种声明方式都可以取得? <img src ="http://www.aygfsteel.com/smcdl/aggbug/298782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/smcdl/" target="_blank">teddy</a> 2009-10-18 22:59 <a href="http://www.aygfsteel.com/smcdl/archive/2009/10/18/298782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于JavaScript的cloneNodeҎ对于节点上的事gclone问题的研I?/title><link>http://www.aygfsteel.com/smcdl/archive/2009/10/18/298743.html</link><dc:creator>teddy</dc:creator><author>teddy</author><pubDate>Sun, 18 Oct 2009 05:28:00 GMT</pubDate><guid>http://www.aygfsteel.com/smcdl/archive/2009/10/18/298743.html</guid><wfw:comment>http://www.aygfsteel.com/smcdl/comments/298743.html</wfw:comment><comments>http://www.aygfsteel.com/smcdl/archive/2009/10/18/298743.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/smcdl/comments/commentRss/298743.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/smcdl/services/trackbacks/298743.html</trackback:ping><description><![CDATA[<p>我的试环境是IE8Q对于事件clone要注意一?点:<br /> 1.q行时添加的事g不会被克隆,如下例:</p> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">1</span><img align="top" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">a </span><span style="color: #000000">=</span><span style="color: #000000"> document.getElementById('a'); </span><span style="color: #008000">//</span><span style="color: #008000">假设a是一个div</span><span style="color: #008000"><br /> </span><span style="color: #008080">2   </span><span style="color: #000000">a.onclick </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">function</span><span style="color: #000000"> () </span><span id="Codehighlighter1_70_95_Open_Text"><span style="color: #000000">{ alert(</span><span style="color: #0000ff">this</span><span style="color: #000000">.innerHTML); }</span></span><span style="color: #000000"><br /> </span><span style="color: #008080">3</span><span style="color: #000000"><img align="top" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" alt="" />b </span><span style="color: #000000">=</span><span style="color: #000000"> a.cloneNode();</span></div> <br /> 2.解析HTML时原来带的事件会被克隆,如下例:<br /> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">1</span><img align="top" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000"><</span><span style="color: #000000">div id</span><span style="color: #000000">=</span><span style="color: #000000">'a' onclick</span><span style="color: #000000">=</span><span style="color: #000000">'alert(</span><span style="color: #0000ff">this</span><span style="color: #000000">.innerHTML)'</span><span style="color: #000000">></span><span style="color: #000000">aaaa</span><span style="color: #000000"></</span><span style="color: #000000">div</span><span style="color: #000000">></span></div> <br /> 3.通过attachEventd的事件会被克隆,如下例:<br /> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">1</span><img align="top" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" alt="" /><span style="color: #000000">a </span><span style="color: #000000">=</span><span style="color: #000000"> document.getElementById('a');<br /> </span><span style="color: #008080">2    </span><span style="color: #000000">a.attachEvent('onclick', </span><span style="color: #0000ff">function</span><span style="color: #000000"> () </span><span id="Codehighlighter1_71_96_Open_Text"><span style="color: #000000">{<br /> </span><span style="color: #008080">3          </span><span style="color: #000000">alert(</span><span style="color: #0000ff">this</span><span style="color: #000000">.innerHTML);<br /> </span><span style="color: #008080">4    </span><span style="color: #000000">}</span></span><span style="color: #000000">);<br /> </span><span style="color: #008080">5</span><span style="color: #000000"><img align="top" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" alt="" />b </span><span style="color: #000000">=</span><span style="color: #000000"> a.cloneNode();</span></div> <br /> 最后如果想在clone时将节点其中的子节点一同clone的话Q请使用cloneNode(true)? <img src ="http://www.aygfsteel.com/smcdl/aggbug/298743.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/smcdl/" target="_blank">teddy</a> 2009-10-18 13:28 <a href="http://www.aygfsteel.com/smcdl/archive/2009/10/18/298743.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>参考Prototype的Class.create写了一个类似的实现Q但是不是Ruby like OOPQ用上更像Java?/title><link>http://www.aygfsteel.com/smcdl/archive/2009/06/09/280773.html</link><dc:creator>teddy</dc:creator><author>teddy</author><pubDate>Mon, 08 Jun 2009 16:04:00 GMT</pubDate><guid>http://www.aygfsteel.com/smcdl/archive/2009/06/09/280773.html</guid><wfw:comment>http://www.aygfsteel.com/smcdl/comments/280773.html</wfw:comment><comments>http://www.aygfsteel.com/smcdl/archive/2009/06/09/280773.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/smcdl/comments/commentRss/280773.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/smcdl/services/trackbacks/280773.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;">实现代码Q?br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> $A (Arguments) {<br /> </span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [];<br /> </span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; i </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);"> Arguments.length; i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">        result.push(Arguments[i]);<br /> </span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> result;<br /> </span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">}<br /> </span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">Class </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {<br /> </span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    create : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> args </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> $A(arguments);<br /> </span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> parentClass </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> args.shift();<br /> </span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> properties;<br /> </span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (parentClass </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> Function)<br /> </span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">            properties </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> args.shift();<br /> </span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (parentClass </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> Object) {<br /> </span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">            properties </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> parentClass;<br /> </span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">            parentClass </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /> </span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">        }<br /> </span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> klazz </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.initialize) </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.initialize.apply(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">, arguments);<br /> </span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">        };<br /> </span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (parentClass) {<br /> </span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> tmpClass </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Function;<br /> </span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">            tmpClass.prototype </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> parentClass.prototype;<br /> </span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">            klazz.prototype </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> tmpClass();<br /> </span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">            klazz.prototype.constructor </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> klazz;<br /> </span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">        }<br /> </span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> key </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> properties) {<br /> </span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (properties[key] </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> Function </span><span style="color: rgb(0, 0, 0);">&&</span><span style="color: rgb(0, 0, 0);"> klazz.prototype[key] </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> Function) {<br /> </span><strong><span style="color: rgb(0, 128, 128);">33</span> </strong><span style="color: rgb(0, 0, 0);"><strong>                </strong><strong>klazz.prototype[key] </strong></span><strong><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">                    </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> _parent </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> klazz.prototype[key];<br /> </span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">                    </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> _method </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> properties[key];<br /> </span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">                    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);">() {<br /> </span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">                        </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> _this </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">;<br /> </span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">                        $super </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);">(){<br /> </span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);">                            _parent.apply(_this, arguments);<br /> </span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">                        }<br /> </span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">                        _method.apply(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">, arguments);<br /> </span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);">                    }<br /> </span><span style="color: rgb(0, 128, 128);">43</span> </strong><span style="color: rgb(0, 0, 0);"><strong>                })();</strong><br /> </span><span style="color: rgb(0, 128, 128);">44</span> <span style="color: rgb(0, 0, 0);">            }<br /> </span><span style="color: rgb(0, 128, 128);">45</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> <br /> </span><span style="color: rgb(0, 128, 128);">46</span> <span style="color: rgb(0, 0, 0);">                klazz.prototype[key] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> properties[key];<br /> </span><span style="color: rgb(0, 128, 128);">47</span> <span style="color: rgb(0, 0, 0);">        }<br /> </span><span style="color: rgb(0, 128, 128);">48</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);">49</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> klazz;<br /> </span><span style="color: rgb(0, 128, 128);">50</span> <span style="color: rgb(0, 0, 0);">    }<br /> </span><span style="color: rgb(0, 128, 128);">51</span> <span style="color: rgb(0, 0, 0);">}</span></div> <br /> 试代码Q?br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);">            T </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Class.create({<br /> </span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">                initialize : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> (tt,mm) {<br /> </span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">                    alert(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">initialize T<img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);">                    </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.tt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> tt;<br /> </span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">                    </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.mm </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mm;<br /> </span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">                },<br /> </span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">                l : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">                    alert(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">f</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">                },<br /> </span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">                f : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">                    alert(</span><span style="color: rgb(0, 0, 0);">123</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">                }<br /> </span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">            });<br /> </span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">            <br /> </span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">            M </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Class.create(T, {<br /> </span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">                initialize : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> (tt,mm,nn) {<br /> </span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">                    $super(tt,mm);<br /> </span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">                    alert(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">initialize M<img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /></span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">                    </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.nn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> nn;<br /> </span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">                },<br /> </span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">                f : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">                    <span style="color: rgb(102, 255, 32);"><span style="color: rgb(0, 255, 15);"><span style="color: rgb(27, 216, 0);">//$super();  q里也可以不调用父类的fҎ</span></span></span><br /> </span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">                    alert(</span><span style="color: rgb(0, 0, 0);">456</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">                },<br /> </span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">                s : </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> () {<br /> </span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">                    alert(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">sssss</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">                }<br /> </span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">            });<br /> </span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">            <br /> </span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">            N </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Class.create(M);<br /> </span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">            <br /> </span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">            m </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> N(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">);<br /> </span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">            m.f();<br /> </span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">            m.l();<br /> </span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">            m.s();<br /> </span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">            alert(m.tt);<br /> </span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">            alert(m.mm);<br /> </span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);">            alert(m.nn);<br /> </span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">            alert(m </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> N);<br /> </span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">            alert(m </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> M);<br /> </span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);">            alert(m </span><span style="color: rgb(0, 0, 255);">instanceof</span><span style="color: rgb(0, 0, 0);"> T);</span><span style="color: rgb(0, 128, 128);"><br /> </span></div> <br /> 在做的过E中Q上面粗体部分不是像现在q样实现的,遇到了一个怪异的问题,下面l出原来的实玎ͼ<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> _parent </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> klazz.prototype[key];<br /> </span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> _method </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> properties[key];<br /> </span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">klazz.prototype[key] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);">() {<br /> </span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">var</span><span style="color: rgb(0, 0, 0);"> _this </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">;<br /> </span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">    $super </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);">(){<br /> </span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);">         _parent.apply(_this, arguments);<br /> </span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">    }<br /> </span><span style="color: rgb(0, 128, 128);">8</span> <span style="color: rgb(0, 0, 0);">    _method.apply(</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">, arguments);<br /> </span><span style="color: rgb(0, 128, 128);">9</span> <span style="color: rgb(0, 0, 0);">}</span></div> q种实现造成了klazz的prototype中的每个Ҏ执行Lq回相同l果Q原因是_parent和_method在create一个Class? 时候已l被固定到properties的最后一个key上,所以在q行的时候会q回相同的结果,解决Ҏ如同上面_体部分Q必ȝ一个函数封装ƈ立即执行 q回内部函数Q这是闭包的一U用方式,q种做法cM把函数内的内容做一个快照,q回的内部函数存取变量的时候其实是从快照中取得Q而不是从 properties中取得?br /> <br /> q个版本q不能用在生产环境中Q由于时间仓促,肯定会有BUG或不完善的地斏V所以徏议大家还是用Prototype的实玎ͼ我这个实现只是用来更加深ȝ理解一些概念,比如变量作用域链、闭包等{的使用?br /> <br /> </span> <img src ="http://www.aygfsteel.com/smcdl/aggbug/280773.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/smcdl/" target="_blank">teddy</a> 2009-06-09 00:04 <a href="http://www.aygfsteel.com/smcdl/archive/2009/06/09/280773.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对JavaScript的变量作用域的理?/title><link>http://www.aygfsteel.com/smcdl/archive/2009/05/19/271421.html</link><dc:creator>teddy</dc:creator><author>teddy</author><pubDate>Mon, 18 May 2009 18:06:00 GMT</pubDate><guid>http://www.aygfsteel.com/smcdl/archive/2009/05/19/271421.html</guid><wfw:comment>http://www.aygfsteel.com/smcdl/comments/271421.html</wfw:comment><comments>http://www.aygfsteel.com/smcdl/archive/2009/05/19/271421.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/smcdl/comments/commentRss/271421.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/smcdl/services/trackbacks/271421.html</trackback:ping><description><![CDATA[<span style="font-size: 10pt;"> <p>作用域是一个抽象概念,在浏览器的JavaScript实现中会有这个概念,但是不同览器实现可能不同?br /> <br /> 一、作用域的种c(自己感悟Q?br /> 1.对象作用域,window及标{օ素都属于对象作用域?br /> 2.函数作用域,函数自n形成的作用域?br /> 可以把作用域看作是上下文QcontextQ之cȝ对象?br /> <br /> 看下面的代码<br /> ?</p> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 98%; font-size: 13px;"><span style="color: #008080;"> 1</span><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff;">breakE</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;"> 2</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff;">var</span><span style="color: #000000;"> state </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">;                </span><span style="color: #008000;">//</span><span style="color: #008000;">1</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 3</span><span style="color: #008000;"><img id="Codehighlighter1_69_245_Open_Image" onclick="this.style.display='none'; Codehighlighter1_69_245_Open_Text.style.display='none'; Codehighlighter1_69_245_Closed_Image.style.display='inline'; Codehighlighter1_69_245_Closed_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display: none;" id="Codehighlighter1_69_245_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_69_245_Closed_Text.style.display='none'; Codehighlighter1_69_245_Open_Image.style.display='inline'; Codehighlighter1_69_245_Open_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="color: #000000;">Person </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;">(name) </span><span style="border: 1px solid #808080; background-color: #ffffff; display: none;" id="Codehighlighter1_69_245_Closed_Text"><img alt="" src="http://www.aygfsteel.com/Images/dot.gif" /></span><span id="Codehighlighter1_69_245_Open_Text"><span style="color: #000000;">{     </span><span style="color: #008000;">//</span><span style="color: #008000;">2</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 4</span><span style="color: #008000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="color: #000000;">    </span><span style="color: #0000ff;">var</span><span style="color: #000000;"> age </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">20</span><span style="color: #000000;">;<br /> </span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.name </span><span style="color: #000000;">=</span><span style="color: #000000;"> name;<br /> </span><span style="color: #008080;"> 6</span><span style="color: #000000;"><img id="Codehighlighter1_147_229_Open_Image" onclick="this.style.display='none'; Codehighlighter1_147_229_Open_Text.style.display='none'; Codehighlighter1_147_229_Closed_Image.style.display='inline'; Codehighlighter1_147_229_Closed_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display: none;" id="Codehighlighter1_147_229_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_147_229_Closed_Text.style.display='none'; Codehighlighter1_147_229_Open_Image.style.display='inline'; Codehighlighter1_147_229_Open_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.show </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () </span><span style="border: 1px solid #808080; background-color: #ffffff; display: none;" id="Codehighlighter1_147_229_Closed_Text"><img alt="" src="http://www.aygfsteel.com/Images/dot.gif" /></span><span id="Codehighlighter1_147_229_Open_Text"><span style="color: #000000;">{     </span><span style="color: #008000;">//</span><span style="color: #008000;">3</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 7</span><span style="color: #008000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="color: #000000;">        alert(age);<br /> </span><span style="color: #008080;"> 8</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        alert(state);<br /> </span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        alert(name);<br />                alert(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.name);<br /> </span><span style="color: #008080;">10</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color: #000000;"><br /> </span><span style="color: #008080;">11</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    alert(m);<br /> </span><span style="color: #008080;">12</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000;"><br /> </span><span style="color: #008080;">13    Person.prototype.check = function () {};</span><span style="color: #000000;"><br /> </span><span style="color: #008080;">14</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />m </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Person('mike');       </span><span style="color: #008000;">//</span><span style="color: #008000;">4</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">15</span><span style="color: #008000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />var </span><span style="color: #000000;">j </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Person('Jerry');      </span><span style="color: #008000;">//</span><span style="color: #008000;">5</span><span style="color: #008000;"><br /> </span><span style="color: #008080;">16</span><span style="color: #008000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #000000;"><br /> </span><span style="color: #008080;">17</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />m.show();                     </span><span style="color: #008000;">//</span><span style="color: #008000;">6</span></div> 通过看上面这D代码我们发现有时候定义变量时用varQ有时候却不用Q这里有什么区别吗Q在此之前我们先定下规矩Q那是必须有一个全局作用域,像是window对象的存在一P是作用域的最层。那么哪些变量被定义C用域的最层呢?那就是stateQ对于不在Q何函数或cM用var定义的变量存在于全局作用域中。那么全局作用域属于作用域的哪U类型呢Q那是对象作用域。具体是哪个对象的作用域呢?非window对象莫属?br /> <br /> 我们可以把对象作用域惛_成两部分Q左面部分是对象U别的,保存的是和对象绑定的成员属性(无论是变量还是函敎ͼQ通过this来访问的。右面的部分是作用域U别的,保存的是var定义的属性(无论是变量还是函敎ͼQ可以直接通过变量名称讉K。对于window来说它是个特D体Q从用var定义的变量可以用this来访问就能看出问题,我个人猜window对象既是JavaScript的顶层对象又是顶层作用域Q所以才会有q种效果?br /> 另一U情况就是标{օ素的对象作用域和全局作用域非常的像,如下例:<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 98%; font-size: 13px;"><span style="color: #008080;"> 1</span><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><span style="color: #008000;">//</span><span style="color: #008000;">?</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 2</span><span style="color: #008000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #000000;"><</span><span style="color: #000000;">input type</span><span style="color: #000000;">=</span><span style="color: #000000;">'button' value</span><span style="color: #000000;">=</span><span style="color: #000000;">'click me' onclick</span><span style="color: #000000;">=</span><span style="color: #000000;">'alert(value)'</span><span style="color: #000000;">/></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 3</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">?</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 4</span><span style="color: #008000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #000000;"><</span><span style="color: #000000;">input type</span><span style="color: #000000;">=</span><span style="color: #000000;">'button' value</span><span style="color: #000000;">=</span><span style="color: #000000;">'click me' onclick</span><span style="color: #000000;">=</span><span style="color: #000000;">'alert(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.value)'</span><span style="color: #000000;">/></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">?</span><span style="color: #008000;"><br /> </span><span style="color: #008080;"> 6</span><span style="color: #008000;"><img id="Codehighlighter1_162_205_Open_Image" onclick="this.style.display='none'; Codehighlighter1_162_205_Open_Text.style.display='none'; Codehighlighter1_162_205_Closed_Image.style.display='inline'; Codehighlighter1_162_205_Closed_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display: none;" id="Codehighlighter1_162_205_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_162_205_Closed_Text.style.display='none'; Codehighlighter1_162_205_Open_Image.style.display='inline'; Codehighlighter1_162_205_Open_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="color: #0000ff;">function</span><span style="color: #000000;"> test() </span><span style="border: 1px solid #808080; background-color: #ffffff; display: none;" id="Codehighlighter1_162_205_Closed_Text"><img alt="" src="http://www.aygfsteel.com/Images/dot.gif" /></span><span id="Codehighlighter1_162_205_Open_Text"><span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 7</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    alert(value);<br /> </span><span style="color: #008080;"> 8</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    alert(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.value);<br /> </span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000;"><br /> </span><span style="color: #008080;">10</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #000000;"><</span><span style="color: #000000;">input type</span><span style="color: #000000;">=</span><span style="color: #000000;">'button' value</span><span style="color: #000000;">=</span><span style="color: #000000;">'click me' onclick</span><span style="color: #000000;">=</span><span style="color: #000000;">'test()'</span><span style="color: #000000;">/></span></div> 很奇怪ؓ什么例1和例2讉Kvalue和全局作用域里的var定义的变量的行ؓ是一致的Q按理说value应该属于input标签元素对象的成员属性,讉K对象的成员属性应该用this来访问,我们自定义的c,如果成员Ҏ惌问成员变量都需要加this的,像最上面的例子里的this.showҎ讉Kthis.name成员变量一栗而对于例3Q勤快的读者如果运行一下便知一定会出错的,W一句系l会报value未定义(除非你在全局作用域定义过value变量Q,W二句会打印出undefined。ؓ什么会q样呢?q就要引Z用域链这个概念了Q后面会解释q个概念的?br /> <br /> 反观函数作用域,׃函数中用var定义的变量不能用this来访问,所以在函数作用域中不再像对象作用域那样分两部分Q而this实际保存的是与该函数l定的对象的引用Q也是说this在函C有特D的含义Q是不能挪作它用的?br /> <br /> 二、作用域?br /> 作用域是可以嵌套的,而嵌套的l果Ş成了作用域链Q如?所C,我们来描qC下作用域链:<br /> 全局作用?lt;----Person函数作用?lt;-----show函数作用?br /> 在全局作用域中可以讉K哪些变量呢(注意我现在指的只是作用域中的变量Q?<br /> 1.state<br /> 2.j<br /> 在Person函数作用域中可以讉K哪些变量呢?<br /> 1.全局作用域中的所有可讉K的变?br /> 2.nameQ参敎ͼ<br /> 3.age<br /> 在show函数中又有哪些变量可以被讉K呢?<br /> 1.全局作用域中的所有可讉K变量<br /> 2.Person函数作用域中所有可讉K变量<br /> 3.如若show函数中还定义了var变量也要加入show函数作用?br /> <br /> 如果在show中访问一个x变量Q首先从show函数作用域里查是否有xq个变量Q如果没有再查Person作用域,如果q没有的话再查全局作用域,扑ֈp回该变量?br /> 讲解到这里我们发C个问题,g作用域是定义时生成块U作用域Q即单独的对象作用域和函C用域Q而运行时生成作用域链Q这是词法作用域的由来。有了这U作用域构成了闭包的基?br /> 下面是一个稍微特D的例子Q?br /> </span> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() {<br /> </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">    test2();<br /> </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">}<br /> </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"><br /> </span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test3() {<br /> </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">    test2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() {<br /> </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">        alert(</span><span style="color: #000000;">"</span><span style="color: #000000;">test2</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br /> </span><span style="color: #008080;"> 8</span> <span style="color: #000000;">    }<br /> </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    test();<br /> </span><span style="color: #008080;">10</span> <span style="color: #000000;">}</span></div> <span style="font-size: 10pt;">定义test的时候test2q不存在Q但是调用test的时候,test2存在了Q这说明了作用域铄实是q行时生成的?/span><br /> <br /> <span style="font-size: 10pt;"> 通过作用域链如何解释???呢?<br /> 我的分析是对于HTML元素来说当页面解析的时候,元素被解析成DOM树,HTML元素里的标准属性作元素对象作用域的变量Q类似var定义的那U)Q而非该元素DOM对象的成员属性(cMthis定义的那U)Q这样就解释了两个问题:<br /> 1.在页面上预定义的HTML元素标准属性是不能用delete和removeAttribute删除的?br /> 2.onclick属于input元素的一个方法,那么q个作用域链应该像下面这P<br />    全局作用?lt;-----HTML元素作用?lt;-------onclick函数作用?br />    value变量在哪个作用域里呢Q答案是HTML元素作用域中。所以onclick不但可以讉K而且加不加this都可以?br /> 那么?的作用域链是什么样的呢Q如下:<br />     全局作用?lt;-----test函数作用?br />     全局作用?lt;-----HTML元素作用?lt;-------onclick函数作用?br /> 没错是两条,因ؓ当单L钮后执行到onclick函数中时走的是第二条链,而在onclick中调用了test函数Ӟ走的是第一条链?br /> 昄valueq是定义在HTML元素作用域中Q而test惌问valueg定是讉K不到的?br /> <br /> 对于q个问题我还试了一下非标准属性,如下例:<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 98%; font-size: 13px;"><span style="color: #008080;">1</span><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><span style="color: #000000;"><</span><span style="color: #000000;">input type</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">button</span><span style="color: #000000;">"</span><span style="color: #000000;"> value</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">click me</span><span style="color: #000000;">"</span><span style="color: #000000;"> bb</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">123</span><span style="color: #000000;">"</span><span style="color: #000000;"> onclick</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">delete this.bb; alert(bb)</span><span style="color: #000000;">"</span><span style="color: #000000;">/></span></div> 试发现非标准的HTML元素属性可以被删除Q但是delete语句执行时必ȝthis来引用(IE中测试)Q直接访问如alert到没有这个限制?br /> 说明bb是input对象属性是没错的,因ؓ它可以被删除Q但是如果它没有被删除的话alert(bb)q是能显C出来的Q说明这cd性很ҎQ具体怎么解释我还没有惛_Q如果有朋友能解释的通,请告知我?br /> <br /> 三、什么是闭包<br /> show函数昄是一个闭包,但是它是q的构造函数生成的Q和另一UŞ式其实是一LQ代码如下:<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 98%; font-size: 13px;"><span style="color: #008080;"> 1</span><img id="Codehighlighter1_24_80_Open_Image" onclick="this.style.display='none'; Codehighlighter1_24_80_Open_Text.style.display='none'; Codehighlighter1_24_80_Closed_Image.style.display='inline'; Codehighlighter1_24_80_Closed_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display: none;" id="Codehighlighter1_24_80_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_24_80_Closed_Text.style.display='none'; Codehighlighter1_24_80_Open_Image.style.display='inline'; Codehighlighter1_24_80_Open_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="color: #0000ff;">function</span><span style="color: #000000;"> getFunc (a, b) </span><span style="border: 1px solid #808080; background-color: #ffffff; display: none;" id="Codehighlighter1_24_80_Closed_Text"><img alt="" src="http://www.aygfsteel.com/Images/dot.gif" /></span><span id="Codehighlighter1_24_80_Open_Text"><span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 2</span><span style="color: #000000;"><img id="Codehighlighter1_49_78_Open_Image" onclick="this.style.display='none'; Codehighlighter1_49_78_Open_Text.style.display='none'; Codehighlighter1_49_78_Closed_Image.style.display='inline'; Codehighlighter1_49_78_Closed_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display: none;" id="Codehighlighter1_49_78_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_49_78_Closed_Text.style.display='none'; Codehighlighter1_49_78_Open_Image.style.display='inline'; Codehighlighter1_49_78_Open_Text.style.display='inline';" alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () </span><span style="border: 1px solid #808080; background-color: #ffffff; display: none;" id="Codehighlighter1_49_78_Closed_Text"><img alt="" src="http://www.aygfsteel.com/Images/dot.gif" /></span><span id="Codehighlighter1_49_78_Open_Text"><span style="color: #000000;">{<br /> </span><span style="color: #008080;"> 3</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />         alert(a </span><span style="color: #000000;">-</span><span style="color: #000000;"> b);<br /> </span><span style="color: #008080;"> 4</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color: #000000;"><br /> </span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span><span style="color: #000000;"> <br /> </span><span style="color: #008080;"> 6</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><br /> </span><span style="color: #008080;"> 7</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff;">var</span><span style="color: #000000;"> func1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> getFunc(</span><span style="color: #000000;">10</span><span style="color: #000000;">,</span><span style="color: #000000;">5</span><span style="color: #000000;">);<br /> </span><span style="color: #008080;"> 8</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />func1();<br /> </span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><br /> </span><span style="color: #008080;">10</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff;">var</span><span style="color: #000000;"> func2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> getFunc(</span><span style="color: #000000;">20</span><span style="color: #000000;">,</span><span style="color: #000000;">30</span><span style="color: #000000;">);<br /> </span><span style="color: #008080;">11</span><span style="color: #000000;"><img alt="" src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />func2();</span></div> 上面的代码func1函数无论我执行多次都显C?Qfunc2函数无论我执行多次都显C?10Q这和例0是多么的像呀Q例0中m.show()无论执行多少ơ都能显C?mike'Qj.show()无论执行多少ơ都能显C?Jerry'Q行?那行在v作用Q先不考虑this.name那行Q。这是闭包Q把作用域链中变化的变量l固定化Q然后把作用域链和函数本w进行绑定,无论我在什么地Ҏ行函敎ͼ函数的作用域铑֧l是它定义的时候那条?br /> <br /> 四、关于对象成员的讉KQ即this能引用的那些属性)Q我只是在猜惻I可能的作用域铑ֺ该如下:<br />     全局作用?lt;-------m对象作用?lt;-------m.test函数作用?br />     m的对象成员(cMthis定义的)保存在对象作用域的左半部Qm的作用域变量Q类中var定义的)保存在对象作用域的右半部?br />     m.test函数要访问对象成员时从对象作用域的左半部索,要访问作用域变量时从对象作用域的叛_部检索?br />     同时q个作用域链q解{了另一个问题,卛_型方法(公共ҎQؓ什么不能访问类中用var定义的变量?br />     看例0卛_知check和PersonҎ׃在同一个链上,又谈何能讉K呢?<br /> <br /> x把JavaScript变量作用域讲解完毕,我觉得这个还是蛮重要的,如果一定要有个比较的话Q应该和Java中的ClassLoader有同L重要性吧。大家借鉴的同时请多给我指出错误,因ؓq其中有一些还是我的猜? </span> <img src ="http://www.aygfsteel.com/smcdl/aggbug/271421.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/smcdl/" target="_blank">teddy</a> 2009-05-19 02:06 <a href="http://www.aygfsteel.com/smcdl/archive/2009/05/19/271421.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IE document compatModehttp://www.aygfsteel.com/smcdl/archive/2009/04/15/265802.htmlteddyteddyWed, 15 Apr 2009 09:37:00 GMThttp://www.aygfsteel.com/smcdl/archive/2009/04/15/265802.htmlhttp://www.aygfsteel.com/smcdl/comments/265802.htmlhttp://www.aygfsteel.com/smcdl/archive/2009/04/15/265802.html#Feedback0http://www.aygfsteel.com/smcdl/comments/commentRss/265802.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/265802.htmlcompatMode Property

Gets a value that indicates whether standards-compliant mode is switched on for the object.

Syntax

 [ sMode = ] object.compatMode

Possible Values

sMode String that receives one of the following values.
BackCompatStandards-compliant mode is not switched on.
CSS1CompatStandards-compliant mode is switched on.

The property is read-only. The property has no default value.

Remarks

With Microsoft Internet Explorer 6 and later, you switch on standards-compliant mode by including the !DOCTYPE declaration at the top of your document, specifying a valid Label in the declaration, and in some cases, specifying the Definition or URL. The Label element specifies the unique name of the Document Type Definition (DTD), and can be appended with the version number of the DTD. The Definition element specifies the definition of the DTD that is specified in the Label. The URL element specifies the location of the DTD.

When standards-compliant mode is switched on (also called "strict mode"), Windows Internet Explorer displays the document according to the World Wide Web Consortium (W3C) Cascading Style Sheets (CSS) World Wide Web link standard. When standards-compliant mode is not switched on (also called "quirks mode"), the document is displayed as it was displayed in previous versions of Internet Explorer.

 New for Internet Explorer 8 The compatMode property is deprecated in Internet Explorer 8 in favor of the documentMode property.




IE8 documentMode

Syntax

[ vMode = ] object.documentMode

Possible Values

vMode Floating-point that receives one of the following values.
5Microsoft Internet Explorer 5 mode (also known as "quirks mode").
7Internet Explorer 7 Standards mode.
8Internet Explorer 8 Standards mode.

The property is read-only. The property has no default value.

Remarks

This property is available in all compatibility modes.

The document compatibility mode of a Web page determines how Internet Explorer 8 interprets and renders the page. Page authors generally use meta elements to specify X-UA-Compatible HTTP-EQUIV headers that specify the document compatibility mode. For more information, see Defining Document Compatibility. In versions of Internet Explorer previous to Internet Explorer 8, compatMode was solely determined by DOCTYPE.



teddy 2009-04-15 17:37 发表评论
]]>
JavaScript对form及form中的引用?/title><link>http://www.aygfsteel.com/smcdl/archive/2009/03/19/260892.html</link><dc:creator>teddy</dc:creator><author>teddy</author><pubDate>Thu, 19 Mar 2009 12:44:00 GMT</pubDate><guid>http://www.aygfsteel.com/smcdl/archive/2009/03/19/260892.html</guid><wfw:comment>http://www.aygfsteel.com/smcdl/comments/260892.html</wfw:comment><comments>http://www.aygfsteel.com/smcdl/archive/2009/03/19/260892.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/smcdl/comments/commentRss/260892.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/smcdl/services/trackbacks/260892.html</trackback:ping><description><![CDATA[通常我们对form的引用通过lform加上id来实玎ͼ其实我们可以使用name来实现。两者的不同是id需要用document.getElementById()来检索,name通过document.forms[form_name]来检索,在form中的表单元素都有name的时候可以通过document.forms[form_name][element_name]来检索到指定的表单元素。另一U方法可以通过document.forms[form_name].elements来获得表单元素的集合Q但是其中不包括input type='image'Q所有这些方法在IE7、Opera、FF2、FF3、Chrome、Safari上测试通过?br /> <br /> <img src ="http://www.aygfsteel.com/smcdl/aggbug/260892.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/smcdl/" target="_blank">teddy</a> 2009-03-19 20:44 <a href="http://www.aygfsteel.com/smcdl/archive/2009/03/19/260892.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JavaScript注意事项Q不定期更新Q?/title><link>http://www.aygfsteel.com/smcdl/archive/2009/01/19/251838.html</link><dc:creator>teddy</dc:creator><author>teddy</author><pubDate>Mon, 19 Jan 2009 02:42:00 GMT</pubDate><guid>http://www.aygfsteel.com/smcdl/archive/2009/01/19/251838.html</guid><wfw:comment>http://www.aygfsteel.com/smcdl/comments/251838.html</wfw:comment><comments>http://www.aygfsteel.com/smcdl/archive/2009/01/19/251838.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/smcdl/comments/commentRss/251838.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/smcdl/services/trackbacks/251838.html</trackback:ping><description><![CDATA[<strong>   本脓不定期更新Q将集合JavaScript使用中常见的问题?/strong><br />   <strong>U定Q浏览器名称IE/FF(通指Windowsq_上的IE、FF)、IE5.5+/FF2+(后面的数字表C版本,甚至可以有更详细的版本信息)<br />   目前只测试IE和FFQ其他浏览器不在试范围内,如果我只提及IE的,则说明在IE中有问题Q而在FF中没有问题?br /> </strong> <ol> <li>IE中Class不能跨窗口引用,卛_父窗口定义的SomeClass不能在子H口中实例化,?strong><span style="color: #ff0000">new opener.SomeClass()</span></strong><span style="color: #ff0000"><span style="color: #000000"><span style="color: #ff0000"><span style="color: #000000">。请注意在这里我没有提到frame的情况,因ؓ我还没有旉试Q如果哪位有旉试请告诉我l果?/span></span></span></span><br /> <li>IE中delete操作只能删除JavaScript或自定义对象的属性,而不能删除HTML Element对象的属性,如果惛_除Element的属性,请?strong><span style="color: #ff0000">removeAttribute()</span></strong>Ҏ? <li>IE中delete操作只能删除非var定义的变量?nbsp; <li>IE中如果在一个窗口引用一个对象,如果q个对象是在其他H口建立的,但是那个H口已经关闭了,那么对这个对象的引用会出错?</li> </ol>           如在H口A?br />           opener.someClassObj = new SomeClass();<br />           window.close();<br />           H口B?br />           typeof someClassObj l果?'object'Q?但是实际引用该对象却会出错?br />          <br /> <br /> <img src ="http://www.aygfsteel.com/smcdl/aggbug/251838.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/smcdl/" target="_blank">teddy</a> 2009-01-19 10:42 <a href="http://www.aygfsteel.com/smcdl/archive/2009/01/19/251838.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在定义一个jscȝ时候,Z么要讄该类的prototype属性ؓ它所要承的cȝ实例对象http://www.aygfsteel.com/smcdl/archive/2008/12/05/244594.htmlteddyteddyFri, 05 Dec 2008 06:24:00 GMThttp://www.aygfsteel.com/smcdl/archive/2008/12/05/244594.htmlhttp://www.aygfsteel.com/smcdl/comments/244594.htmlhttp://www.aygfsteel.com/smcdl/archive/2008/12/05/244594.html#Feedback0http://www.aygfsteel.com/smcdl/comments/commentRss/244594.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/244594.html function A() {}
function B() {}
B.prototype = new A()

上面q样写是Z让instanceof语句能v作用。D个例子:
1.不重写子cȝprototype属?br /> b = new B();
b instanceof B  //return true
b instanceof A // return false
b instanceof Object //return true

2.写子cȝprototype属?br /> b = new B();
b instanceof B //return true
b instanceof A //return true
b instanceof Object //return true

另外Qprototype的作用是可以用来模拟l承Q我们在父c里d的属性方法在子类里能讉K?br /> 但是我们可以使用一U其他的Ҏ来变通?br />
function A(x) {
    this.x = x;
    this.method1 = functioni () {};
}

function B(x,y) {
    A.call(this,x);
    this.y = y;
}

b  =  new B(1, 2)

q时b中绝ҎxQƈ且x {于1Q但是我们ؓ什么还要用prototype呢?
主要是ؓ了向父类原型动态添加的属性和Ҏ可以出现在子cȝ实例对象中?br /> 接着上面的例?br />
A.prototype.z = function () {}

如果我们没有讄B.prototype = new A()Q则b不会动态添加方法z ?br />


teddy 2008-12-05 14:24 发表评论
]]>
JavaScript cd转换注意事项http://www.aygfsteel.com/smcdl/archive/2008/11/14/240447.htmlteddyteddyFri, 14 Nov 2008 01:32:00 GMThttp://www.aygfsteel.com/smcdl/archive/2008/11/14/240447.htmlhttp://www.aygfsteel.com/smcdl/comments/240447.htmlhttp://www.aygfsteel.com/smcdl/archive/2008/11/14/240447.html#Feedback1http://www.aygfsteel.com/smcdl/comments/commentRss/240447.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/240447.htmlJavaScript: The Definitive Guide, 5th Edition?br />
字符型{换ؓ数值型可以使用下列Ҏ
parseInt(stringVar);     //parseIntq可以指定二q制、八q制、或16q制
parseFloat(stringVar);
Number(stringVar);

for example:
parseInt("ff")    //will throw a error
parseInt("ff", 16) == parseInt("0xff")     // return 255
parseInt("77")    //return 77
parseInt("077") == parseInt("77", 8)    // return 63
注:?0"?0x"前缀会自动检ƈ转换为相应的数制所表示的?br /> If parseInt( ) or parseFloat( ) cannot convert the specified string to a number, it returns NaN

数值型转换为字W型
numberVar + "";
numberVar.toString();   //可以指定二进制、八q制、或16q制
String(numberVar);

other useful method
var n = 123456.789;
n.toFixed(0); // "123457"
n.toFixed(2); // "123456.79"
n.toExponential(1); // "1.2e+5"
n.toExponential(3); // "1.235e+5"
n.toPrecision(4); // "1.235e+5"
n.toPrecision(7); // "123456.8"

其实q有一U方法就是用new操作W,?br /> new String(numberVar);
new Numer(stringVar);
但是q种Ҏq回的结果是objectcdQ而不是原始数据类型,大家可以酌情使用?br />
另外Q在相等判断时? == '会自动{型(具体转换情况请参考其他资料)Q如果想_判断请? === '?br /> ?nbsp;  1 == '1'    //return true
       1 === '1' //return false
       1 == new Number(1)   //return true
       1 === new Number(1)   //return false

数D接等相关操作
"21" + "2" == "21" + 2  //return 212
2 + "21"   //return 221
"21" * "2" == "21" * 2 == 2 * "21"  //return 42
"21" / "3" == "21" / 3 == 21 / "3"  //return 7
"21" - "2" == "21" - 2  == 21 - "2" == 21 - "    2    "  //return 19

正如和Java中一Pnew Number(3) == new Number(3)q回falseQ同理推q到其他cdQnew操作WL建立一个新对象Q?br /> ?=只是比较其引用,q不比较对象本nQ所以两个new的对象的引用L不同的。所以在通常的逻辑判断?如if或while{?Q?br /> 最好避免用Primitive Datatype WrapperQ而直接用Primitive Datatype?br />
From Wrapper to Primitive, for example:
new Number(3).valueOf()
new String("str").valueOf()
new Date().valueOf()        //convert a Date to millisecond representation
[any other object].valueOf()    //The primitive value associated with the object, if any. If there is no value associated with object, returns the object itself.

Finally, note that any number, string, or boolean value can be converted to its corresponding wrapper object with the Object( ) function:

var number_wrapper = Object(3);



Automatic datatype conversions

Value

Context in which value is used


String

Number

Boolean

Object

Undefined value

"undefined"

NaN

false

Error

null

"null"

0

false

Error

Nonempty string

As is

Numeric value of string or NaN

TRue

String object

Empty string

As is

0

false

String object

0

"0"

As is

false

Number object

NaN

"NaN"

As is

false

Number object

Infinity

"Infinity"

As is

true

Number object

Negative infinity

"-Infinity"

As is

TRue

Number object

Any other number

String value of number

As is

true

Number object

true

"true"

1

As is

Boolean object

false

"false"

0

As is

Boolean object

Object

toString( )

valueOf( ), toString( ), or NaN

true

As is


利用上面的表Q我们可以知道if ("") 、if (0)、if (undefined)、if (null)、if (NaN)Q这些if语句的条仉为false.





teddy 2008-11-14 09:32 发表评论
]]>
面向对象的JavaScriptQ二QTSS上的一文章,先借鉴一下)http://www.aygfsteel.com/smcdl/archive/2008/09/17/229519.htmlteddyteddyWed, 17 Sep 2008 12:15:00 GMThttp://www.aygfsteel.com/smcdl/archive/2008/09/17/229519.htmlhttp://www.aygfsteel.com/smcdl/comments/229519.htmlhttp://www.aygfsteel.com/smcdl/archive/2008/09/17/229519.html#Feedback0http://www.aygfsteel.com/smcdl/comments/commentRss/229519.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/229519.html Object Oriented JavaScript Demonstrated



teddy 2008-09-17 20:15 发表评论
]]>
面向对象的JavaScript Q一、对象基Q用函数来模拟c)http://www.aygfsteel.com/smcdl/archive/2008/09/03/226748.htmlteddyteddyWed, 03 Sep 2008 11:09:00 GMThttp://www.aygfsteel.com/smcdl/archive/2008/09/03/226748.htmlhttp://www.aygfsteel.com/smcdl/comments/226748.htmlhttp://www.aygfsteel.com/smcdl/archive/2008/09/03/226748.html#Feedback1http://www.aygfsteel.com/smcdl/comments/commentRss/226748.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/226748.html
  1function Person(name, age) {
  2
  3    //U有变量(private variables)
  4    var myName = name;
  5    var myAge = age;
  6
  7    //公共属?public properties)
  8    this.name = myName;
  9    this.age = myAge;
 10    
 11    //U有函数(private functions)
 12    function getMyName() {
 13    return myName;
 14    }
;
 15    
 16    function getName() {
 17    return name;
 18    }
;
 19    
 20    var getMyAge = function () {
 21    return myAge;
 22    }
;
 23    
 24    var getAge = function () {
 25    return age;
 26    }
;
 27    
 28    //ҎҎ(privileged methods)
 29    this.getName = function () {
 30    return this.name;
 31        //return myName;       可以讉KU有变量
 32    //return name;         可以讉K构造函数参?其参数本质也是私有变?
 33    //return getMyName();  可以讉KU有函数
 34    //return getName();    可以讉KU有函数
 35    }
;
 36    
 37    this.getAge = function () {      
 38    return getAge();   
 39    }
;
 40    
 41    this.getRealAge = function () {
 42    return this.age;
 43    }
;
 44}

 45
 46//公共Ҏ(public methods)
 47Person.prototype.acceptName = function (param) {
 48    this.name = param;
 49    //name = param;       公共Ҏ不能讉KU有变量
 50    //myName = param;     公共Ҏ不能讉KU有变量
 51}
;
 52
 53//原型属?prototype properties)
 54Person.prototype.legs = 2;
 55
 56//静态属?static properties)
 57//整个cd有一个静态属性,各个对象׃n同一个静态属性?/span>
 58Person.arms = 2;
 59
 60Person.getArms = function () {
 61    return Person.arms;
 62    //return this.arms;   可以使用this来指代Person对象本n
 63}
;
 64
 65/*
 66Person是一个逻辑上的c,他的本质是一个FunctionQ在JavaScript中类是由Function来模拟的Q所以Personq是一个Function的实例。而arms和getArmsҎ是一个Function对象实例上的属性和ҎQ是Person对象所Ҏ的,所以本例可以用Person.arms来引用,同时在getArmsҎ中,可以使用this.arms来引用,因ؓgetArms是Person对象上的ҎQ在该方法内部this指代Person对象自n?br />  67*/

 68alert(Person.getArms());
 69
 70var pa = new Person("Teddy"25);
 71
 72//alert(pa.myName);  不能讉KU有变量
 73
 74alert(pa.name);
 75
 76alert(pa.getAge());
 77
 78pa.acceptName("Born");
 79
 80alert(pa.getName());
 81
 82var pb = new Person("John"18);
 83
 84//q行时动态加入方?/span>
 85Person.prototype.acceptAge = function (param) {
 86    this.age = param;
 87}
;
 88
 89//pb对象也可以调用动态添加的Ҏ
 90pb.acceptAge(30);
 91
 92alert(pb.getAge());
 93
 94alert(pb.getRealAge());
 95
 96//q行时覆盖已定义Ҏ
 97Person.prototype.acceptName = function (param) {
 98    return param;
 99}
;
100
101//所有已创徏对象都自动承被覆盖的方?/span>
102alert(pa.acceptName("Black"));
103
104alert(pa.getName());
105
106alert(pa.legs);
107
108//q行时改变原型属性?/span>
109Person.prototype.legs = 4;
110
111//所有已创徏对象都自动承被改变的原型属性?/span>
112alert(pb.legs);
113
114//q行时将原型属性改为公共方?/span>
115Person.prototype.legs = function () {
116    return 4;
117}
;
118
119//所有已创徏对象都自动承原型属性到公共Ҏ的改?/span>
120alert(pa.legs());


teddy 2008-09-03 19:09 发表评论
]]>
兼容IE和FF的Javascript动态添加Select控g的Option选项http://www.aygfsteel.com/smcdl/archive/2007/12/30/171771.htmlteddyteddySun, 30 Dec 2007 14:49:00 GMThttp://www.aygfsteel.com/smcdl/archive/2007/12/30/171771.htmlhttp://www.aygfsteel.com/smcdl/comments/171771.htmlhttp://www.aygfsteel.com/smcdl/archive/2007/12/30/171771.html#Feedback0http://www.aygfsteel.com/smcdl/comments/commentRss/171771.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/171771.html selectObj.add(new Option("Label", "Value"));
但是addҎ好像只在IE下好用。现在提供一个通用的方法:
selectObj.options[selectObj.length] = new Option("Label", "Value");
另外如果惌想用addҎQ可以写成如下格式:
selectObj.add(new Option("Label", "Value"), null);
h意,在IE6及IE7下请使用不带null参数的addҎQ在FF下请使用带null参数的方法,IE8下带不带都可以?br /> 很奇怪ؓ什么一定要加nullQ我猜测addҎ里一定用了'=== null'来判断第二的参数或者没有对参数数组的长度做验证?br />

teddy 2007-12-30 22:49 发表评论
]]>
关于IE与FF对HtmlElement属性操作的不同http://www.aygfsteel.com/smcdl/archive/2007/09/12/144618.htmlteddyteddyWed, 12 Sep 2007 13:17:00 GMThttp://www.aygfsteel.com/smcdl/archive/2007/09/12/144618.htmlhttp://www.aygfsteel.com/smcdl/comments/144618.htmlhttp://www.aygfsteel.com/smcdl/archive/2007/09/12/144618.html#Feedback4http://www.aygfsteel.com/smcdl/comments/commentRss/144618.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/144618.html 但是有些不可以,在FF下必ȝelement.getAttribute(attributeName)来读取?br /> q且QIE下这三种方式的行为是一致的Q而FF下前两种和后一U是不一致的?br /> 同理讄属性的Ҏ也是不一致的?br /> 其内部的实现原理q不得而知Q希望有知道的朋友指点一二?

teddy 2007-09-12 21:17 发表评论
]]>
关于JavaScript的Prototype更进一步的讨论http://www.aygfsteel.com/smcdl/archive/2007/05/26/120133.htmlteddyteddySat, 26 May 2007 00:49:00 GMThttp://www.aygfsteel.com/smcdl/archive/2007/05/26/120133.htmlhttp://www.aygfsteel.com/smcdl/comments/120133.htmlhttp://www.aygfsteel.com/smcdl/archive/2007/05/26/120133.html#Feedback1http://www.aygfsteel.com/smcdl/comments/commentRss/120133.htmlhttp://www.aygfsteel.com/smcdl/services/trackbacks/120133.html本文属于中等U别的文章,如果大家对OOP of JavaScriptq不清楚的话Q请先去了解一下,
写本文的原因是习惯了Java、C#{语aQ对于JavaScript的prototypeQ通常所说的原型Q非?
不适应Q很多朋友可能都有这L体验Q这文章能解答你们很多的同c问题?nbsp; 阅读全文

teddy 2007-05-26 08:49 发表评论
]]>
վ֩ģ壺 | | | ɽʡ| | | | | | | | | | ɽ| Ͻ| | | Ͷ| ƽ| ĺ| | ƽ| е| ɽ| | ׺| ³ľ| ˱| | | | ̨ǰ| ƽ| | | ɽ| | ʡ| | ½| |