I am implementing a drag'n'drop interface in js which has two tables of objects and a single object as source or destination. To execute the "move" operation, I thought of using these two class methods:
_getVar(id) {
var ind;
switch (id[0]) {
case 'c':
return this.clipboard;
case 's':
ind = Number(id.substr(1)) - 1;
return this.SynthPatches[ind];
case 'f':
ind = Number(id.substr(1)) - 1;
return this.FilePatches[ind];
}
}
move(from, to) {
var fv = this._getVar(from);
var tv = this._getVar(to);
if (fv == undefined) return "Source empty";
if (tv == undefined) return "Destination undefined";
tv = fv;
return "Ok";
}
This does not work. fv and tv are local references to the correct objects. The reference fv is copied to tv and then both are forgotten. I would like something like a deep copy in python, but it looks like the only solution is to inline _getVar() nine times, which makes for really ugly code.
tv = fv;that will not behave as you expect, it will just assign the local variable tv the value of fv, so tv will no more point to it's original object, and nothing happens outside of that function, you need a refference example:mainObj.tv = fv;ormainObj[index].tv = fv;