0

I have added to the String class three prototype classes on a file classed parse.js:

String.prototype.parseCropTo = function (needle) {
    if (this.includes(needle) === false) return false;
    return this.slice(0, this.indexOf(needle)).trim();
}

String.prototype.parseCropFrom = function (needle) {
    if (this.includes(needle) === false) return false;
    return this.slice(this.indexOf(needle) + needle.length).trim();
}

String.prototype.parseCropBetween = function (needleFrom, needleTo) {
    let haystack = this.parseCropFrom(needleFrom);
    if (haystack != false) return haystack.parseCropTo(needleTo);
}

As far as I can see, imported files have to expose specific functions and then they are called via a variable. However, I wish to import parse.js to other files so I could use these functions directly on strings:

let haystack = 'This is a lovely day';
console.log(haystack.parseCropBetween('is', 'day'));

Is this possible? thanks in advance.

1 Answers1

1

By extending the prototype of String, you will have these methods on every string you'll ever use, however you need to load it somewhere in your code, because you won't be able to use that beforehand.

The reason that works is because your'e accessing String.prototype by reference, as with all non-primitive types in javascript so calling it once, will get you set for the rest of your code.

Generally speaking, it's not advised to extend native constructs.

See full example here: https://codesandbox.io/s/epic-cerf-4qshv?file=/src/App.js

Additionally, I'd advise you to read some opinions about extending prototypes in javascript and considers the pros and cons of this approach: Why is extending native objects a bad practice?

silicakes
  • 6,364
  • 3
  • 28
  • 39
  • Thanks! Is there a way to create my own class that could be applied on a string, the same way the String class is applied? – Idan Aharoni Feb 25 '21 at 19:56
  • What you're looking for is a function, and to be more specific - a paradigm called "functional programming" which is very different from OOP. The basic idea of it is that you pass your value(s) and perform an action on it in a way that doesn't affected from any external state. Here's a fairly good read in regards to JS: https://opensource.com/article/17/6/functional-javascript – silicakes Feb 26 '21 at 10:44
  • This is how I would implement your code using the above paradigm: https://codesandbox.io/s/nostalgic-pascal-fjkfq?file=/src/index.js – silicakes Feb 26 '21 at 10:59
  • Thanks! This is how it is implemented right now. I was interested to know if there was an implementation that could be used as a method on a string. Not because it is necessarily better, but because I wanted to familiarize myself better with the language. Thanks again – Idan Aharoni Mar 02 '21 at 08:59
  • Then you might consider creating a custom class with the extensions you want. Something like this: https://codesandbox.io/s/inspiring-voice-x7381?file=/src/index.js – silicakes Mar 02 '21 at 13:43