Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Created January 17, 2018 18:02
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 samoshkin/3e60710dc1d222376d539b4281b6669d to your computer and use it in GitHub Desktop.
Save samoshkin/3e60710dc1d222376d539b4281b6669d to your computer and use it in GitHub Desktop.
Custom toString() and valueOf() methods
var obj = {
prop: 101,
toString(){
return 'Prop: ' + this.prop;
},
valueOf() {
return this.prop;
}
};
console.log(String(obj)); // 'Prop: 101'
console.log(obj + '') // '101'
console.log(+obj); // 101
console.log(obj > 100); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment