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
  • 15,331
  • 6
  • 50
  • 88
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
  • 568
  • 4
  • 17