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 () {
            console.log("valueOf1");
            return 1;
        }
    };
    var obj2 = {
        valueOf: function () {
            console.log("valueOf2");
            return 2;
        }
    };

And now:
obj1 + obj2
    valueOf1
    valueOf2
    3
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.
+

Code:

See also:


file: /Techref/language/java/script/operator-overloading.htm, 4KB, , updated: 2023/5/11 21:41, local time: 2024/11/24 20:16, owner: JMN-EFP-786,
TOP NEW HELP FIND: 
3.12.36.45:LOG IN

 ©2024 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="http://ecomorder.com/Techref/language/java/script/operator-overloading.htm"> (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 ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .