2

I have this sample function, and i want to call it in this format :

const myfunc = (string) => {
   return string.length
}

console.log("check_length".myfunc())

How can i do that? Thanks for answers in advance!

bafix2203
  • 541
  • 7
  • 25

1 Answers1

4

The only way is to mutate the prototype of String with a classic function for accessing this.

String.prototype.myfunc = function () {
   return this.length;
};

console.log("check_length".myfunc());
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392