Returning a property of a JavaScript object by value NOT NOT

So I have a class foo that has a method that returns a panel of an array. I have another function that calls foo.getBar and then filters the array. I want to be able to always get the original content of the bar when I use another filter, but bing seems to just create a reference to bar, not a separate array. I tried using return this.bar.valueOf (); in my foo function, still doesn't work. When I remove items from the bin, they are removed from the bar as well. Someone please enlighten me on creating a unique array instead of a reference.

function foo(x, y, z){

    this.bar = new Array();
    ...
    this.bar = [ some , stuff , in , bar ];

    this.getBar = function getBar(){
        return this.bar;    
    }
    ...
}

var FooObject = new foo(x,y,z);

function baz(){

    var bing = FooObject.getBar();

    bing.splice(remove some pieces of the array);
}

      

+2


source to share


5 answers


The easiest (and as far as I know, the fastest) way to get a copy of an array is to use the slice method. With no arguments, it defaults to array.slice(0, array.length)

, so it will copy the entire array.

Your getBar function will look like this:



this.getBar = function getBar(){
    return this.bar.slice();        
}

      

Note that this is a shallow copy, so any changes to objects in the array will affect the original (adding and removing elements won't affect it though).

+5


source


For objects, use the clone method:



function cloneObject(source) {
    for (i in source) {
        if (typeof source[i] == 'source') {
            this[i] = new cloneObject(source[i]);
        }
        else {
            this[i] = source[i];
        }
    }
}

var obj1= {bla:'blabla',foo:'foofoo',etc:'etc'};

var obj2= new cloneObject(obj1);

      

+2


source


What you need to do is something like the following: passing a function as a parameter and forcing a pass-by-value;

function foo(x, y, z) {
    this.bar = ['uno', 'dos', 'tres'];
}
foo.prototype.getBar = function() {
    return this.bar;
}
...
function getBar(fn) {
    return fn();
}
...
var f = new foo(x, y, z);
var bing = getBar(f.getBar);

      

0


source


Returning a "clone" ensures that the original array is not touched. Please note that such a clone will be shallow.

function foo(x, y, z){

    this.bar = [ some , stuff , in , bar ];
    ...
    this.getBar = function getBar(){
       return this.bar.concat([]);
    }
    ...
}

      

0


source


Unfortunately, javascript arrays and objects are always passed by reference. If you are guaranteed that your array foo.bar

is 1-dimensional / contains no arrays or objects,

Then you can do:

var bing = FooObject.getBar().slice(0);

      

Which a 1-fold copy will do foo.bar

, as a result your array bing

is independent of the array foo.bar

.

Otherwise you will have to flip / find a deep copy method like the $ A function in mootools

var newArray = $A(oldArray)

      

0


source







All Articles