如何解决函数返回要分配的数组项
我正在js中实现一个拖放接口,该接口具有两个对象表和一个对象作为源或目标。为了执行“移动”操作,我想到了使用以下两个类方法:
_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";
}
这不起作用。 fv和tv是对正确对象的本地引用。将参考fv复制到电视,然后将两者都遗忘。 我想要类似python的深层副本,但是似乎唯一的解决方案是将_getVar()内联九次,这使得代码非常丑陋。
解决方法
在此处减少重复的最简单方法可能是在函数中添加可选的第二个参数,并允许其设置和获取,例如:
_getOrSetVar(id,value) {
var ind;
switch (id[0]) {
case 'c':
if (value !== undefined) this.clipboard = value
return this.clipboard;
break;
case 's':
ind = Number(id.substr(1)) - 1;
if (value !== undefined) this.SynthPatches[ind] = value
else return this.SynthPatches[ind];
break;
case 'f':
ind = Number(id.substr(1)) - 1;
if (value !== undefined) this.FilePatches[ind] = value
else return this.FilePatches[ind];
break;
}
}
并执行以下更新:
this._getOrSetVar(to,this._getOrSetVar(from))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。