Answered
In JavaScript, how do I apply a method directly to a string?
So I can use something like this:
"string".doSomething()
And have it work on any string?
This creates a upperCaseFirstLetter()
method you can apply to strings:
String.prototype.upperCaseFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1)
}
And you can use it like this:
"bananas".upperCaseFirstLetter() // "Bananas"
You can create a custom string method like this:
String.prototype.yourMethodName = function() {
return this // string is accessed via this
}