Value types versus .valueOf

Alex Vincent ajvincent at gmail.com
Mon Jun 9 18:09:14 PDT 2014


I like the idea of value types, but I wonder if, for terminating decimal
numbers, the .valueOf() method from Object.prototype might be sufficient
for many operations.

Example:

function SmallDecimal(k) {
    this.__value__ = k;
}

SmallDecimal.prototype.valueOf = function() {
    return this.__value__.toString(10);
}

var x = new SmallDecimal(2);
var y = new SmallDecimal(3);
[x < y, x > y, x == y, x <= y, x >= y, x != y]

/*
true,false,false,true,false,true
*/

I still want to see value types go forward:  in the above example, x + y
would result in "23", where x - y === -1.  Perhaps .valueOf could be used
in testing simple value types implementations.
-- 
"The first step in confirming there is a bug in someone else's work is
confirming there are no bugs in your own."
-- Alexander J. Vincent, June 30, 2001
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140609/6f89ea16/attachment-0001.html>


More information about the es-discuss mailing list