JSFiddle - React, Tailwind, and code Playground
HTML
<div id="result"></div>
JavaScript
var superString = (function () {
// constructor
function MyString(str) {
this._str = str;
}
// crisr uma cópia para não escrever métodos na String nativa
MyString.prototype = String.prototype;
MyString.prototype = new MyString();
// funcionalidade "append"
MyString.prototype.append = function (value) {
return this.toString() + value;
};
// é preciso sobreescrever estes métodos...
MyString.prototype.toString = function () {
return this._str;
};
MyString.prototype.valueOf = function () {
return this._str;
};
Object.defineProperty(MyString.prototype, 'comprimento', {
get: function () {
return this._str.length;
}
});
return MyString;
})();
var str = new superString('Concatena com ');
document.getElementById('result').textContent = str.append('isto!');
console.log(str.comprimento); // 14