How to call your method on a string?

  • 0

    How it works? Why are we calling methods on the string? Where are they generally defined?
    It looks like extensions like in c # and I tried to write my own:

    Unfortunately, it doesn't work.
    JavaScript Anonymous, Nov 4, 2019

  • 2 Answers
  • 0
    The primitive is wrapped in an object of the corresponding type and uses the methods of its prototype. In fact:

    (new String ('hello')). toUpperCase ();
    Emmett Copeland

  • 0
    An instance of the String object is created with a toUpperCase () method, the methods are defined inside String.prototype (try typing in the console). To add your own method, extend the prototype of the desired object:
    String.prototype.myLength = function( arg ) {
    // ваш код

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!