Skip to content

Instantly share code, notes, and snippets.

@tsouk
Last active January 21, 2018 21:58
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 tsouk/0d3498cb111b3e7099c57070fd2de171 to your computer and use it in GitHub Desktop.
Save tsouk/0d3498cb111b3e7099c57070fd2de171 to your computer and use it in GitHub Desktop.
Quirky JS
var a = {
i: 1,
// toString: function () {
// return a.i++;
// }
valueOf: function () {
return a.i++;
}
// When using loose equality, due to type coersion, if one of the operands is of a different type than the other, the engine will attempt to convert one to the other. In the case of an object on the left and a number on the right, it will attempt to convert the object to a number by first calling valueOf if it is callable, and failing that, it will call toString
// every time one of these is called, i is being incemented. The == operator calls valueOf then toString
}
if ( a==1 && a==2 && a==3 ) {
console.log("Hello Quirky 1!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment