Re: Window load/unload calling Object.prototype extension method.



Thomas 'PointedEars' Lahn wrote:
Garrett Smith wrote:
<script type="text/javascript">
Object.prototype.zvt_hello = function() {
alert(this.constructor);
}

var d = new Date();
d.zvt_hello();
var i = new Number();
i.zvt_hello();
</script>

- results in two alerts:

function Date(){
[native code]
}

function Number(){
[native code]
}

<aside>
Those don't conform to ECMA 262r3

You are mistaken because alert() is a host method (of Window objects, and
should therefore be called window.alert()). It may display anything there.



The alert method converts its argument to a string value.

window.alert( '' + this.constructor );

would have the same effect of popping up an alert box with the [native code] function representations.

<aside>
Although this is invalid, certain libraries actually depend on this behavior. Dojo, Mootools, and Qooxdoo, for example perform tests to see if the result of converting an object to a string (using its toString) contains "[native code]".

This practice is not just non-standard, even if the [native code] were allowed it is fallible on two counts that have been discussed.
</aside>

Garrett
.



Relevant Pages