<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: 小心数组的length</title>
	<atom:link href="http://lifesinger.org/blog/2009/01/be-careful-about-array-length/feed/" rel="self" type="application/rss+xml" />
	<link>http://lifesinger.org/blog/2009/01/be-careful-about-array-length/</link>
	<description>关注用户体验、前端开发，记录生活点滴、岁月足迹。</description>
	<lastBuildDate>Thu, 09 Sep 2010 07:06:13 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: zhxlTony</title>
		<link>http://lifesinger.org/blog/2009/01/be-careful-about-array-length/comment-page-1/#comment-573</link>
		<dc:creator>zhxlTony</dc:creator>
		<pubDate>Wed, 04 Mar 2009 02:35:28 +0000</pubDate>
		<guid isPermaLink="false">http://lifesinger.org/blog/?p=1024#comment-573</guid>
		<description>Ecma-262的p97-98有一段叙述了数组put时的过程：

&lt;blockquote&gt;
  Assume A is an Array object and P is a
  string. When the [[Put]] method of A
  is called with property P and value V,
  the following steps are taken:
  1. Call the [[CanPut]] method of A with name P.
  2. If Result(1) is false, return.
  3. If A doesn’t have a property with name P, go to step 7.
  4. If P is &quot;length&quot;, go to step 12.
  5. Set the value of property P of A to V.
  6. Go to step 8.
  7. Create a property with name P, set its value to V and give it empty
  attributes.
  8. If P is not an array index, return.
  9. If ToUint32(P) is less than the value of the length property of A,
  then return.
  10. Change (or set) the value of the length property of A to ToUint32(P)+1.
  11. Return.
  12. Compute ToUint32(V).
  13. If Result(12) is not equal to ToNumber(V), throw a RangeError
  exception.
  14. For every integer k that is less than the value of the length property
  of A but not less than Result(12), if
  A itself has a property (not an
  inherited property) named ToString(k),
  then delete that property.
  - 98 -
  15. Set the value of property P of A to Result(12).
  16. Return.
&lt;/blockquote&gt;

可以看出，对于ToUint32(P)!=P的情况，只进行到8.就return了，不会改变A.length。
而10.中也可以看出，当ToUint32(P)&gt;=A.length的时候，就改变A的length为ToUint32(P)+1了。</description>
		<content:encoded><![CDATA[<p>Ecma-262的p97-98有一段叙述了数组put时的过程：</p>
<blockquote><p>
  Assume A is an Array object and P is a<br />
  string. When the [[Put]] method of A<br />
  is called with property P and value V,<br />
  the following steps are taken:<br />
  1. Call the [[CanPut]] method of A with name P.<br />
  2. If Result(1) is false, return.<br />
  3. If A doesn’t have a property with name P, go to step 7.<br />
  4. If P is &#8220;length&#8221;, go to step 12.<br />
  5. Set the value of property P of A to V.<br />
  6. Go to step 8.<br />
  7. Create a property with name P, set its value to V and give it empty<br />
  attributes.<br />
  8. If P is not an array index, return.<br />
  9. If ToUint32(P) is less than the value of the length property of A,<br />
  then return.<br />
  10. Change (or set) the value of the length property of A to ToUint32(P)+1.<br />
  11. Return.<br />
  12. Compute ToUint32(V).<br />
  13. If Result(12) is not equal to ToNumber(V), throw a RangeError<br />
  exception.<br />
  14. For every integer k that is less than the value of the length property<br />
  of A but not less than Result(12), if<br />
  A itself has a property (not an<br />
  inherited property) named ToString(k),<br />
  then delete that property.<br />
  &#8211; 98 -<br />
  15. Set the value of property P of A to Result(12).<br />
  16. Return.
</p></blockquote>
<p>可以看出，对于ToUint32(P)!=P的情况，只进行到8.就return了，不会改变A.length。<br />
而10.中也可以看出，当ToUint32(P)&gt;=A.length的时候，就改变A的length为ToUint32(P)+1了。</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 无语</title>
		<link>http://lifesinger.org/blog/2009/01/be-careful-about-array-length/comment-page-1/#comment-569</link>
		<dc:creator>无语</dc:creator>
		<pubDate>Sat, 24 Jan 2009 17:40:42 +0000</pubDate>
		<guid isPermaLink="false">http://lifesinger.org/blog/?p=1024#comment-569</guid>
		<description>不看不知道，一看吓一跳。
平时没注意到..........</description>
		<content:encoded><![CDATA[<p>不看不知道，一看吓一跳。<br />
平时没注意到&#8230;&#8230;&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lifesinger</title>
		<link>http://lifesinger.org/blog/2009/01/be-careful-about-array-length/comment-page-1/#comment-570</link>
		<dc:creator>lifesinger</dc:creator>
		<pubDate>Fri, 23 Jan 2009 20:20:43 +0000</pubDate>
		<guid isPermaLink="false">http://lifesinger.org/blog/?p=1024#comment-570</guid>
		<description>&lt;p&gt;感谢realazy指点，明白了^o^&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>感谢realazy指点，明白了^o^</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: realazy</title>
		<link>http://lifesinger.org/blog/2009/01/be-careful-about-array-length/comment-page-1/#comment-571</link>
		<dc:creator>realazy</dc:creator>
		<pubDate>Fri, 23 Jan 2009 17:46:30 +0000</pubDate>
		<guid isPermaLink="false">http://lifesinger.org/blog/?p=1024#comment-571</guid>
		<description>JS 没有关联数组的概念，都是直接把对象当作关联数组（因为它提供了 key/value 的适用性）来用的。不过有个陷阱，因为 Array 本身也是对象，同样可以设置 key/value，容易混淆。

记住 JS 没有关联数组，就知道为何有 length 的误解了。你总结的第二点中实际上是要计算除了对象本身属性（ownProperty）外的 key/value pair 的数量。遗憾的是，JS 内置也没有相应的办法。

推荐你看：
http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/</description>
		<content:encoded><![CDATA[<p>JS 没有关联数组的概念，都是直接把对象当作关联数组（因为它提供了 key/value 的适用性）来用的。不过有个陷阱，因为 Array 本身也是对象，同样可以设置 key/value，容易混淆。</p>
<p>记住 JS 没有关联数组，就知道为何有 length 的误解了。你总结的第二点中实际上是要计算除了对象本身属性（ownProperty）外的 key/value pair 的数量。遗憾的是，JS 内置也没有相应的办法。</p>
<p>推荐你看：<br />
<a href="http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/" rel="nofollow">http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 宇义</title>
		<link>http://lifesinger.org/blog/2009/01/be-careful-about-array-length/comment-page-1/#comment-572</link>
		<dc:creator>宇义</dc:creator>
		<pubDate>Fri, 23 Jan 2009 17:42:25 +0000</pubDate>
		<guid isPermaLink="false">http://lifesinger.org/blog/?p=1024#comment-572</guid>
		<description>除了 foo[&#039;3&#039;] 与 foo[3] 效果一样没有想到外，别的结果和我想的一样。</description>
		<content:encoded><![CDATA[<p>除了 foo['3'] 与 foo[3] 效果一样没有想到外，别的结果和我想的一样。</p>
]]></content:encoded>
	</item>
</channel>
</rss>
