I need to implement a function that is called as a string, how to do this better? An example of a call might look like this:
var a = ‘Hi’.myFunc();
console.log(a); // Hi
I need to implement a function that is called as a string, how to do this better? An example of a call might look like this:
var a = ‘Hi’.myFunc();
console.log(a); // Hi
String.prototype.myFunc= function(){
return this.toString()
}
var a = 'Hi'.myFunc();
console.log(a); // Hi
Here is your solution to attach a function to any String
String.prototype.coucou = function(){
console.log("coucou")
}
"a".coucou() //print "coucou" in console