Created
March 11, 2019 11:45
-
-
Save JoshCheek/c45972271f2f0b6b80f5c85d43c43580 to your computer and use it in GitHub Desktop.
JavaScript equality algorithm is terrible.
This file contains 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
class User { | |
constructor(name, age) { this.name = name, this.age = age } | |
[Symbol.toPrimitive](hint) { | |
// hint = "default", regardless of whether we compare to string or number | |
// ...also, how is an object not a primitive? Its got its own literal syntax ffs | |
console.log({args: [...arguments]}) | |
return this.name // we dont know what we need, we just guess here | |
} | |
} | |
const Sara = new User("Sara", 44) | |
;[ Sara == Sara, | |
Sara == new User("Sara", 44), // fucking algorithm just returns false for this... | |
Sara == "Sara", // here it will cast using toPrimitive, but wont hint string | |
Sara == 44, // doesnt hint number here | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment