Skip to content

Instantly share code, notes, and snippets.

@ccnokes ccnokes/valueOf.js
Last active Aug 31, 2016

Embed
What would you like to do?
//custom string manipulation class
class MyString {
constructor(str) {
this.str = str;
}
replaceVowels(replacement) {
this.str = this.str.replace(/a|e|i|o|u/gi, replacement);
}
//The valueOf() method returns the primitive value of the specified object.
valueOf() {
return this.str;
}
}
//instantiate it
let myStr = new MyString('test');
//call custom method
myStr.replaceVowels('*');
//now interact with it like it's a normal string
let newStr = 'hi there ' + myStr;
//=> "hi there t*st"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.