On Sat, 15 Jan 2011 17:38:24 +0100, Michael Haufe (TNO)
<[email protected]> wrote:
Here is a contrived example, but with some imagination I think some
utility could be applied to this.
var a = 6
var b = Number(6)
That's the same as "a".
var c = new Number(6)
Number.prototype.valueOf = function(){
throw "Illegal usage."
}
console.log(a + 3) //9
console.log(b + 3) //9
console.log(c + 3) //Illegal usage
I think the following much better shows the same possibilities but with
more
readability and more room for modelling.
function MyNumLikeType(num) {
this.num = num;
}
MyNumLikeType.prototype.valueOf = function () { return this.num; }
function MyNonNumType(num) {
this.num = num;
}
MyNonNumType.prototype.valueOf = function () { throw "Illegal usage."; }
var a = 6;
var b = new MyNumLikeType(6);
var c = new MyNonNumType(6);
console.log(a + 3) //9
console.log(b + 3) //9
console.log(c + 3) //Illegal usage
/L 'Tilting at windmills. Primitive wrapper constructors are useless,
damnit! Because I say so!'
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]