i'm trying to extend built-in JS String class using ES6 class.
I.e. just to inherit from String.
However, the code below does not work.
+= operator change myStr actual type back to String.
AFAIK, there is no operator inheritance in JS.
So, how to avoid this behaviour?
class QString extends String {
isEmpty() {
return this.length === 0;
}
}
var testStr = new QString();
console.log(testStr.isEmpty());
testStr += new QString("abc");
console.log(testStr.isEmpty()); // 'TypeError: testStr.isEmpty is not a function'
P.S. I know about opportunity to add a new method String.prototype.
But this is a bad practice.
UPD: i understood what there is no + operator overloading in JS, and original answer said it clearly.
However, it said nothing about inheritance and ES6 classes at all.
I think at least accepted answer should be saved.