Skip to content

Instantly share code, notes, and snippets.

@xavi-
Created February 21, 2010 18:27
Show Gist options
  • Save xavi-/310450 to your computer and use it in GitHub Desktop.
Save xavi-/310450 to your computer and use it in GitHub Desktop.
(function() { // Make numbers look like strings
var oldToString = Number.prototype.toString;
Number.prototype.toString = function() {
if(this == 4) { return "four"; }
else { return oldToString.call(this); }
};
})();
var four = new Number(4);
alert(1 + four); // shows 5
alert(four); // shows "four"
(function() { // Make strings look like numbers
var oldValueOf = String.prototype.valueOf;
String.prototype.valueOf = function() {
if(this.toLowerCase() == "five") { return 5; }
else { return oldValueOf.call(this); }
};
})();
var five = new String("five");
alert(1 + five); // shows 6
alert(five); // shows "five"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment