How to call your method on a string?
-
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 -
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 -
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 ) {
// ваш код
};Anonymous
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!