please dont rip this site

(Fake) Operator Overloading

Because Javascript will call the .valueOf method for an object to coerce the value before applying most operators (like basic math ops, binary ops, comparison ops), you can get tricky and change the value returned on the left to the result of a different operation.

    var obj1 = {
        valueOf: function () {
            return 1;
    var obj2 = {
        valueOf: function () {
            return 2;

And now:
obj1 + obj2
Because these are called in order, obj1 can set a flag in the prototype and also store it's value in the prototype, but return nothing. Then obj2 will know it's not first, and can do the actual operation, and return the desired complete value, using obj1's value and it's own.


See also:

file: /Techref/language/java/script/operator-overloading.htm, 4KB, , updated: 2023/5/11 22:41, local time: 2023/9/26 20:04, owner: JMN-EFP-786,

 ©2023 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> (Fake) Operator Overloading</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!