Side-effects of some Array methods ...

Pratap Lakshman (VJ#SDK) pratapl at microsoft.com
Fri Jun 27 06:51:49 PDT 2008


I was wondering about the side effects of calling some of the array methods.
Specifically, consider steps 4, 9, from the 'pop' method:

15.4.4.6        Array.prototype.pop ( )
The last element of the array is removed from the array and returned.
1.      Call the [[Get]] method of this object with argument "length".
2.      Call ToUint32(Result(1)).
3.      If Result(2) is not zero, go to step 6.
4.      Call the [[Put]] method of this object with arguments "length" and Result(2).
5.      Return undefined.
6.      Call ToString(Result(2)-1).
7.      Call the [[Get]] method of this object with argument Result(6).
8.      Call the [[Delete]] method of this object with argument Result(6).
9.      Call the [[Put]] method of this object with arguments "length" and (Result(2)-1).
10.     Return Result(7).
NOTE
The pop function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the pop function can be applied successfully to a host object is implementation-dependent.

The side effect is as follows: if "this" does not have a "length" property, it ends up getting one; if "this" does have a length property, but is not an Array, that "length" property will get updated.
What is the rationale for this?

Example:
<script>
function pizza() {}
var p = new pizza();

document.write("p.length: " + p.length + "<br>");
Array.prototype.pop.call(p);
document.write("p.length: " + p.length + "<br>");

function bridge(len) {
  this.length = len;
}
var b = new bridge(2);

document.write("b.length: " + b.length + "<br>");
Array.prototype.pop.call(b);
document.write("b.length: " + b.length + "<br>");
</script>

pratap

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20080627/fabd3a00/attachment-0002.html 


More information about the Es4-discuss mailing list