Created
March 1, 2012 22:03
-
-
Save easierbycode/1953550 to your computer and use it in GitHub Desktop.
object to primitive coersion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var foo = { | |
| toString: function () { | |
| return 5; | |
| }, | |
| valueOf: function () { | |
| return "foo"; | |
| } | |
| }; | |
| alert(foo.toString() + 1); // 6 (bad!) | |
| alert(foo + 1); // "foo1" (no good!) | |
| alert(+foo); // NaN (the worst!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment