Javascript prototype: possible to change value of reference object?

Asked

Viewed 310 times

2

EDIT: It’s usually at the end, but the question was poorly worded and I decided to switch to this example that is much better. Follow the 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);   
}

For testing...

function testeNada() {
    var Nada = ["Eu","Você","Nós"];
    Nada.nada();
    console.log("Resultado de Nada: ");
    console.log(Nada);
}

In Safari, the internal Out result is the same as the Start Array (Nothing) and the Nothing test result is an empty Array!

In the case of pre-existing prototype 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, it is not possible to modify oneself.

As already mentioned below in the comments, except this = Arr the method works. So I rephrase the question: is there any way to really change the reference object or just by applying methods to itself?

  • 1

    I don’t understand why you want to assign a value to this, even more than in your example this value is itself... If you take this line your code already works as you want.

  • In fact, I don’t think I was happy with that question. I’ll withdraw. Thank you.

  • I re-asked the whole question with a simpler and more direct example.

  • Yet I still don’t understand what you’re trying to do! Your new example does exactly what I expected of it. There’s something that’s not clear to you, but I haven’t figured it out yet.

  • The first time I tried to explain why I came to this point. I take advantage of functions to create methods, but in the end you can not simply take the result and "stick" inside the object. The code will have to be modified as explained in picossi below. So I modified the question and left only the concept to others to look for with that kind of doubt. I have several methods that I will have to modify to have the object present the result at the end, is part!

2 answers

2


You have two ways to do this:

If you use the this directly will be changing the ref obj.

Array.prototype.empurre = function(valor){
    this.push(valor);
};
var UmaArray = new Array();
UmaArray.empurre("umValor");

The second way is similar to the implementation of Rotate

Array.prototype.empurre = function(valor){
    var push = Array.prototype.push;
    push.apply(this, [valor]);
};
var UmaArray = new Array();
UmaArray.empurre("umValor");
  • I reread the question that wasn’t clear. I think your answer means that the only way is to always apply over "this", right?

  • yes, that’s right :D

1

The reason why the first example returns [] it is because In is a pointer of this and every iteration of the loop you’re doing In.pop().

To change the array itself you can do so:

Array.prototype.inverter = function() {
    for (let i = 0, l = this.length; i < l; i++) {
        this[i] = this[i].split('').reverse().join('');
    }
}
var array = ["Eu", "Você", "Nós"];
array.inverter();
console.log("Resultado de Nada: ");
console.log(array); // ["uE", "êcoV", "sóN"]

Basically, methods that change the this do what you ask. All methods of Array that change the original object (as .pop(), .shift(), etc) change the this within that function added to prototype.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.