How "this" in methods of Javascript Number objects be converted to the numeric value of the object? -
it seems in number objects, numeric values can referenced straight using "this" instead of this.valueof() or "this.value", example:
number.prototype.printplusone=function(){ var tmp=this+1; alert(tmp); } var n=new number("5"); n.printplusone();
and why possible(using "this" straight instead of "this.valueof()" in add-on above)? didn't find feature in number specification, did missed something? , farther more, automatic conversion feature used in other classes of objects?
neither operand of +
operator string, javascript calls valueof
implicitly convert object (a number
, in case) primitive value, in order perform numeric addition. quoting david flanagan:
if object has valueof()
method returns primitive value, javascript converts (if necessary) primitive value number , returns result.
so, this
implicitly converted primitive value.
if did silly override number.valueof
:
number.prototype.valueof = function() { homecoming 10; }
then function alert 11 instead.
javascript
No comments:
Post a Comment