Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created March 11, 2019 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/c45972271f2f0b6b80f5c85d43c43580 to your computer and use it in GitHub Desktop.
Save JoshCheek/c45972271f2f0b6b80f5c85d43c43580 to your computer and use it in GitHub Desktop.
JavaScript equality algorithm is terrible.
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