Code can help you.
To solve this problem completely, I think we need something like operator reload . We can reload operators like "+ - / *" and check if the operand is a number, if not, and then throw an error.
As a partial solution, when JavaScript performs an operation of type "a + b", it is called by the valueOf , which inherits from Object.prototype , we can rewrite Object.prototype.valueOf .
Object.prototype.originalValueOf = Object.prototype.valueOf; Object.prototype.valueOf = function() { if (typeof this !== 'number') { throw new Error('Object is not a Number'); } return this.originalValueOf(); } var a = 1 + 2;
(hint: you can delete the generated code and add it to your development environment.)
Jerry Dec 12 '13 at 5:05 2013-12-12 05:05
source share