Is it possible to create your own custom, I believe the term is ‘method’? For example, something like this:
var str = "test"
str.replaceSpecial();
where replaceSpecial() will automatically replace say, the letter e with something else.
The reason I’m interested in doing this is because what I want to do is grab strings and then run a large number of replace actions, so I’m hoping that when I call on replaceSpecial() it will run a function.
Thanks
You can add your methods to
String.prototypewhich will become available to all strings. That is howtrim()is implemented in most libraries for example.However, note that it is generally a bad practice to define very specific functionality on the native prototypes. The above is a good example of exactly the same problem. For such specific cases, consider using a custom function or wrapper to do the job.
or under a custom namespace, for example.