0

I created an extension of the String object to be able to add some functions.

class Text extends String {
// ...
}

But I have a problem. Is it possible to define the value of the character string from methods added in the object ? (If it is impossible I do not understand why.)

If you have an answer (or an explanation for this possible blocking) to my question thank you in advance. (Sorry if i don't express myself well, i don't speak english)

Storm 7953
  • 99
  • 1
  • 6
  • 1
    I mostly understand what you're trying to do, but can you give example code you've tried which didn't work as expected? –  Jul 14 '20 at 09:38
  • https://stackoverflow.com/questions/30257915/extend-a-string-class-in-es6 – Nicolae Maties Jul 14 '20 at 09:39
  • Do you mean `new Text("definition of characters in the instance")`? But no, strings are immutable, you cannot change the string inside an instance. – Bergi Jul 14 '20 at 10:42
  • You probably know the **substring** method. The mozilla documentation says : ``The substring() method returns the part of the string between the start and end indexes, or to the end of the string.``. I want to create a function that does the same thing as substring, for example, but that applies it directly instead of returning it. – Storm 7953 Jul 14 '20 at 12:26
  • And if I cannot change the string inside an instance, why ? – Storm 7953 Jul 14 '20 at 12:30
  • @Storm7953 This is indeed not possible with a string subclass. Write a string *wrapper* instead, and you'll be able to set the value that you are wrapping to a different string. – Bergi Jul 14 '20 at 12:39

0 Answers0