Just for study purposes, I'd like to create a function with dot format (I don't know what is the name of this type of function). I'd like to understand how can I recreate this situation:
String('any string').toLowerCase()
// output ANY STRING
I have this example of mine:
class MyCustomString {
constructor(value) {
this.value = value
}
toUpperCase() {
this.value = String(this.value).toUpperCase();
return this;
}
replace(valueToReplaced, newValue) {
this.value = String(this.value).replace(valueToReplaced, newValue);
return this
}
}
Then, I'd like to run:
MyCustomString('name').toUpperCase().replace('A', 'x');
and the expected output should be:
// output NXME
Can someone help me with how to build a function like this?
That's important to notice that my goal is not to create a new method
String
or to includeprototypes
to its structure (in this example, I've used the methodString
but could beNumber
,Array
or whatever else method), but understanding how this chain of methods is built and mighty applying it to my projects. My intention is not to override any native method or create a new prototype.