<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/extend-Array-prototype-indexOf-problem/</link>
<title><![CDATA[扩展Array.prototype.indexOf而引发的问题]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[前端开发]]></category>
<pubDate>Wed, 10 Nov 2010 03:22:22 +0000</pubDate> 
<guid>https://www.heckjj.com/extend-Array-prototype-indexOf-problem/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 微软雅黑;">因为Array没有indexOf方法，这样在一个数组中查找某个元素的索引时比较麻烦，为了调用方便，于是通过prototype原型扩展了Array.prototype.indexOf()，这样用起来就比较方便了。<br/>Array.prototype.indexOf = function(item) &#123;<br/>for (var i = 0; i &lt; this.length; i++) &#123;<br/>if (this[i] == item)<br/>return i;<br/>&#125;<br/>return -1;<br/>&#125; <br/><br/>用的时候直接<br/>var arr=[1,2,3,4,5];<br/>var index=arr.indexOf(1); //index==0<br/><br/>扩展了以后，用起来很爽很方便，一片和谐景象，但是某次是遍历数组元素的时候，使用for..in..循环，引发了其他的问题，打破了这个和谐的氛围。<br/>var a=[&quot;张飞&quot;,&quot;关羽&quot;,&quot;刘备&quot;,&quot;吕布&quot;];<br/>for(var p in a)&#123;<br/>&nbsp;&nbsp;document.write(p+&quot;=&quot;+a[p]+&quot;&lt;br/&gt;&quot;);<br/>&#125;</span><span style="font-family: 微软雅黑;"><br/><br/>本来想输出这四个人的名字，结果输出的是什么呢？<br/><br/>输出的居然是：</span><textarea name="code" class="js" rows="15" cols="100">
//0=张飞
//1=关羽
//2=刘备
//3=吕布
//indexOf=function(item)
&#123;
&nbsp;&nbsp;for (var i = 0; i &lt; this.length; i++)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if (this[i] == item) return i;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return -1;
&#125;</textarea><br/><span style="font-family: 微软雅黑;"><br/>除了把名字打出来以外，还额外输出了自己扩展的方法indexOf，但是令人疯狂的是，firefox却是“正常”的，只有四个人的人名，为什么会这样？<br/>输出indexOf，自己扩展的，可以理解，毕竟for..in是遍历一个对象的所有用户定义的属性或者一个数组的所有元素。那么firefox为什么不会？后来查了资料才明白，Array在javascript1.6版本已经支持Array.indexOf()，而我用的firefox是3.5版本，已经支持javascript1.8了，indexOf是其Array本身固有的方法了。<br/><br/>而IE，即使我用的是IE8，也才支持到javascript1.3版本。所以IE8认为indexOf是“用户定义的属性”，而firefox认为是自己原生支持的固有的属性。真的是这样吗？做个实验，把indexOf更名为myIndexOf，再试试，结果IE和firefox都输出myIndexOf，证明前面的观点是正确。那么又来了个问题，我扩展indexOf很久了，现在不少项目的代码都已经在使用这个方法，而现在我非要使用for..in输出数组本身的元素，不要其他我自己扩展到俄方法，怎么办？<br/><br/>好在javascript提供了hasOwnProperty方法。<br/>看一下其描述：<br/>Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object’s prototype chain<br/><br/>看描述，就是我们想要的东西。在for…in..里做个 判断就OK了</span><textarea name="code" class="js" rows="15" cols="100">
if(a.hasOwnProperty(p))&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(p+&quot;=&quot;+a[p]+&quot;&lt;br/&gt;&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;</textarea><br/><br/><span style="font-family: 微软雅黑;">另外，附上hasOwnProperty用法示例，来源于互联网：</span><textarea name="code" class="js" rows="15" cols="100">
function Book(title, author) &#123;
&nbsp;&nbsp; this.title = title;
&nbsp;&nbsp; this.author = author;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp; Book.prototype.price = 9.99;
&nbsp;&nbsp; Object.prototype.copyright = &quot;idcpw.com&quot;;
&nbsp;&nbsp; var myBook = new Book(&quot;JavaScript Tutorials&quot;, &quot;Jony Heck&quot;);
&nbsp;&nbsp; // Dumping built-in properties at the base prototype level
&nbsp;&nbsp; document.writeln(&quot;&#92;nObject.prototype&#039;s built-in properties:&quot;);
&nbsp;&nbsp; dumpProperty(Object.prototype, &quot;constructor&quot;);
&nbsp;&nbsp; dumpProperty(Object.prototype, &quot;hasOwnProperty&quot;);
&nbsp;&nbsp; dumpProperty(Object.prototype, &quot;isPrototypeOf&quot;);
&nbsp;&nbsp; dumpProperty(Object.prototype, &quot;toString&quot;);
&nbsp;&nbsp; dumpProperty(Object.prototype, &quot;valueOf&quot;);
&nbsp;&nbsp; dumpProperty(Object.prototype, &quot;copyright&quot;);
&nbsp;&nbsp; // Dumping built-in properties at the my prototype level
&nbsp;&nbsp; document.writeln(&quot;&#92;n==================&#92;nBook.prototype&#039;s built-in properties:&quot;);
&nbsp;&nbsp; dumpProperty(Book.prototype, &quot;constructor&quot;);
&nbsp;&nbsp; dumpProperty(Book.prototype, &quot;hasOwnProperty&quot;);
&nbsp;&nbsp; dumpProperty(Book.prototype, &quot;isPrototypeOf&quot;);
&nbsp;&nbsp; dumpProperty(Book.prototype, &quot;toString&quot;);
&nbsp;&nbsp; dumpProperty(Book.prototype, &quot;valueOf&quot;);
&nbsp;&nbsp; dumpProperty(Book.prototype, &quot;copyright&quot;);
&nbsp;&nbsp; // Dumping built-in properties at the object level
&nbsp;&nbsp; document.writeln(&quot;&#92;n==================&#92;nmyBook&#039;s built-in properties:&quot;);
&nbsp;&nbsp; dumpProperty(myBook, &quot;constructor&quot;);
&nbsp;&nbsp; dumpProperty(myBook, &quot;hasOwnProperty&quot;);
&nbsp;&nbsp; dumpProperty(myBook, &quot;isPrototypeOf&quot;);
&nbsp;&nbsp; dumpProperty(myBook, &quot;toString&quot;);
&nbsp;&nbsp; dumpProperty(myBook, &quot;valueOf&quot;);
&nbsp;&nbsp; dumpProperty(myBook, &quot;copyright&quot;);

function dumpProperty(object, property) &#123;
&nbsp;&nbsp; var inheritance;
&nbsp;&nbsp; if (object.hasOwnProperty(property))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inheritance = &quot;Local&quot;;
&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inheritance = &quot;Inherited&quot;;
&nbsp;&nbsp; document.writeln(property+&quot;: &quot;+inheritance+&quot;: &quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+object[property]);
&#125;</textarea><br/><span style="font-family: 微软雅黑;">查看浏览器支持javascript到哪个版本：</span><textarea name="code" class="js" rows="15" cols="100">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&nbsp;&nbsp;&lt;title&gt;浏览器的JavaScript版本支持测试&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
&nbsp;&nbsp; &lt;script language=&quot;JavaScript&quot;&gt;
&nbsp;&nbsp; //document.write(&quot;您的浏览器类型：&quot;+navigator.appName+&quot;&lt;br/&gt;&quot;);
&nbsp;&nbsp; //document.write(&quot;浏览器版本：&quot;+navigator.appVersion+&quot;&lt;br/&gt;&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.0的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.0&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.1&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.1的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.1&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.2&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.2的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.2&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.3&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.3的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.3&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.4&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.4的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.4&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.5&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.5的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.5&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.6&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.6的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.6&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script&nbsp;&nbsp; language=&quot;JavaScript1.7&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.7的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.7&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.8&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript 1.8的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.8&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;script language=&quot;JavaScript1.9&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//支持JavaScript1.9的浏览器才能够执行该脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.write(&#039;该浏览器支持JavaScript1.9&lt;br/&gt;&#039;);
&nbsp;&nbsp;&lt;/script&gt;
 &lt;/body&gt;
&lt;/html&gt;</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/array/" rel="tag">array</a> , <a href="https://www.heckjj.com/tags/firefox/" rel="tag">firefox</a> , <a href="https://www.heckjj.com/tags/ie/" rel="tag">ie</a> , <a href="https://www.heckjj.com/tags/javascript/" rel="tag">javascript</a> , <a href="https://www.heckjj.com/tags/prototype/" rel="tag">prototype</a> , <a href="https://www.heckjj.com/tags/%25E5%2585%25BC%25E5%25AE%25B9/" rel="tag">兼容</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/extend-Array-prototype-indexOf-problem/#blogcomment</link>
<title><![CDATA[[评论] 扩展Array.prototype.indexOf而引发的问题]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/extend-Array-prototype-indexOf-problem/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>