EDIT: Normally it is in the end, but the question was poorly formulated and I decided to switch to this example, which is much better. Here is an example of a function that does nothing as an Array method:
Array.prototype.nada = function () {
var In = this;
var Out = new Array();
for (var i=0; In.length>0; i++) { Out.push(In.pop()); }
In = Out;
console.log("Resultado interno: ");
console.log(Out);
}
To test ...
function testeNada() {
var Nada = ["Eu","Você","Nós"];
Nada.nada();
console.log("Resultado de Nada: ");
console.log(Nada);
}
In Safari, the inner Out result is the same Array as the Start (Nothing) and the Nothing test result is an empty Array!
In the case of prototypes preexisting methods, it works like this:
var UmaArray = new Array();
UmaArray.push("umValor"); // UmaArray já é ["umValor"]
By logic, I would do so:
Array.prototype.empurre = function(valor){
Arr = this;
Arr.push(valor);
this = Arr; // Aqui dá erro ReferenceError: Left side of assignment is not a reference
}
var UmaArray = new Array();
UmaArray.empurre("umValor"); // Sonho de ver UmaArray ser ["umValor"]
That is, you can not modify yourself.
As already mentioned in the comments below, taking this = Arr method works. So I rephrase the question: is there any way to actually change the reference object by just applying methods on it myself?