1

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
Alex
  • 131
  • 3

2 Answers2

3
String.prototype.myFunc= function(){
   return this.toString()
}

var a = 'Hi'.myFunc();
console.log(a); // Hi
Naeem Shaikh
  • 14,881
  • 6
  • 48
  • 86
1

Here is your solution to attach a function to any String

String.prototype.coucou = function(){
 console.log("coucou")   
}

"a".coucou()  //print "coucou" in console
Nolyurn
  • 523
  • 2
  • 16