I'm implementing a method extension library of prototype
, and if I do it simply everything works perfectly as you can see in the simple example below:
String.prototype.append = function(value) {
// aqui o this é a instancia da String. Ok!
return this.toString() + value;
};
document.getElementById('result').textContent = "Concatena com ".append("isto!");
<p id="result">
</p>
But to avoid overwriting methods of prototype
, I created an object within prototype
to register these methods, but with that the scope of the method changes and the this
is no longer a reference to String
, as can be seen in the following example:
String.prototype.extencion = {
append: function(value) {
// aqui o this não é mas uma instancia da String. Fail =(!
// Como pegar a instancia do objecto pai?
return this.toString() + value;
}
};
document.getElementById('result').textContent = "Concatena com ".extencion.append("isto!");
<p id="result"></p>
Question
Is it possible to retrieve the instance of the parent object in a function
in the child object?