mozilla

Revision 111951 of Array

  • Revision slug: Talk:JavaScript/Reference/Global_Objects/Array
  • Revision title: Array
  • Revision id: 111951
  • Created:
  • Creator: AdmiralNovia
  • Is current revision? No
  • Comment Array constructor behavior

Revision Content

iterative array methods (also works on strings) library for browsers that support Object.prototype.call:

if (!Array.prototype.indexOf)
	Array.prototype.indexOf = function(item, startIndex) {
		var len = this.length;
		if (startIndex == null)
			startIndex = 0;
		else if (startIndex < 0) {
			startIndex += len;
			if (startIndex < 0)
				startIndex = 0;
		}
		for (var i = startIndex; i < len; i++) {
			var val = this[i] || this.charAt && this.charAt(i);
			if (val == item)
				return i;
		}
		return -1;
	};

if (!Array.prototype.lastIndexOf)
	Array.prototype.lastIndexOf = function(item, startIndex) {
		var len = this.length;
		if (startIndex == null || startIndex >= len)
			startIndex = len - 1;
		else if (startIndex < 0)
			startIndex += len;
		for (var i = startIndex; i >= 0; i--) {
			var val = this[i] || this.charAt && this.charAt(i);
			if (val == item)
				return i;
		}
		return -1;
	};

if (!Array.prototype.forEach)
	Array.prototype.forEach = function(func, thisVal) {
		var len = this.length;
		for (var i = 0; i < len; i++)
			func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this);
	};

if (!Array.prototype.map)
	Array.prototype.map = function(func, thisVal) {
		var len = this.length;
		var ret = new Array(len);
		for (var i = 0; i < len; i++)
			ret[i] = func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this);
		return ret;
	};

if (!Array.prototype.filter)
	Array.prototype.filter = function(func, thisVal) {
		var len = this.length;
		var ret = new Array();
		for (var i = 0; i < len; i++) {
			var val = this[i] || this.charAt && this.charAt(i);
			if(func.call(thisVal, val, i, this))
				ret[ret.length] = val;
		}
		return ret;
	};

if (!Array.prototype.every)
	Array.prototype.every = function(func, thisVal) {
		var len = this.length;
		for (var i = 0; i < len; i++)
			if (!func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this))
				return false;
		return true;
	};

if (!Array.prototype.some)
	Array.prototype.some = function(func, thisVal) {
		var len = this.length;
		for (var i = 0; i < len; i++)
			if (func.call(thisVal, this[i] || this.charAt && this.charAt(i), i, this))
				return true;
		return false;
	};

--Maian 20:26, 11 October 2005 (PDT)

Array constructor behavior

From the article:

When you specify a single parameter with the Array constructor, the behavior depends on whether you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag:

That's great, except that it doesn't tell us what the behavior is outside of web browsers, such as when SpiderMonkey is embedded in another application. AdmiralNovia 11:42, 17 February 2006 (PST)

Revision Source

<p>iterative array methods (also works on strings) library for browsers that support Object.prototype.call:
</p>
<pre>if (!Array.prototype.indexOf)
	Array.prototype.indexOf = function(item, startIndex) {
		var len = this.length;
		if (startIndex == null)
			startIndex = 0;
		else if (startIndex &lt; 0) {
			startIndex += len;
			if (startIndex &lt; 0)
				startIndex = 0;
		}
		for (var i = startIndex; i &lt; len; i++) {
			var val = this[i] || this.charAt &amp;&amp; this.charAt(i);
			if (val == item)
				return i;
		}
		return -1;
	};

if (!Array.prototype.lastIndexOf)
	Array.prototype.lastIndexOf = function(item, startIndex) {
		var len = this.length;
		if (startIndex == null || startIndex &gt;= len)
			startIndex = len - 1;
		else if (startIndex &lt; 0)
			startIndex += len;
		for (var i = startIndex; i &gt;= 0; i--) {
			var val = this[i] || this.charAt &amp;&amp; this.charAt(i);
			if (val == item)
				return i;
		}
		return -1;
	};

if (!Array.prototype.forEach)
	Array.prototype.forEach = function(func, thisVal) {
		var len = this.length;
		for (var i = 0; i &lt; len; i++)
			func.call(thisVal, this[i] || this.charAt &amp;&amp; this.charAt(i), i, this);
	};

if (!Array.prototype.map)
	Array.prototype.map = function(func, thisVal) {
		var len = this.length;
		var ret = new Array(len);
		for (var i = 0; i &lt; len; i++)
			ret[i] = func.call(thisVal, this[i] || this.charAt &amp;&amp; this.charAt(i), i, this);
		return ret;
	};

if (!Array.prototype.filter)
	Array.prototype.filter = function(func, thisVal) {
		var len = this.length;
		var ret = new Array();
		for (var i = 0; i &lt; len; i++) {
			var val = this[i] || this.charAt &amp;&amp; this.charAt(i);
			if(func.call(thisVal, val, i, this))
				ret[ret.length] = val;
		}
		return ret;
	};

if (!Array.prototype.every)
	Array.prototype.every = function(func, thisVal) {
		var len = this.length;
		for (var i = 0; i &lt; len; i++)
			if (!func.call(thisVal, this[i] || this.charAt &amp;&amp; this.charAt(i), i, this))
				return false;
		return true;
	};

if (!Array.prototype.some)
	Array.prototype.some = function(func, thisVal) {
		var len = this.length;
		for (var i = 0; i &lt; len; i++)
			if (func.call(thisVal, this[i] || this.charAt &amp;&amp; this.charAt(i), i, this))
				return true;
		return false;
	};
</pre>
<p>--<a href="User:Maian">Maian</a> 20:26, 11 October 2005 (PDT)
</p>
<h3 name="Array_constructor_behavior"> Array constructor behavior </h3>
<p>From the article:
</p>
<dl><dd><i>When you specify a single parameter with the Array constructor, the behavior depends on whether you specify LANGUAGE="JavaScript1.2" in the &lt;SCRIPT&gt; tag:</i>
</dd></dl>
<p>That's great, except that it doesn't tell us what the behavior is outside of web browsers, such as when SpiderMonkey is embedded in another application. <a href="User:AdmiralNovia">AdmiralNovia</a> 11:42, 17 February 2006 (PST)
</p>
Revert to this revision