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!

1
  • 1
    So you would have to extend the string prototype Commented Jul 1, 2019 at 15:46

1 Answer 1

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());

Sign up to request clarification or add additional context in comments.

1 Comment

Correct. Also, please, never ever actually do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.