Javascript 复制现有对象

Javascript 复制现有对象,javascript,Javascript,我有点困惑 基本上,framerjs将我的photoshop文档导出到HTML5网站。所有元素似乎都在PSD对象中。我可以通过PSD[元素名称]处理我的文档中的每个项目。Framer调用这些视图。因此,可以通过myNewView=newview创建一个新视图;视图可以包含文本、图像等。到目前为止,还不错 我正在尝试以下操作:从每个现有视图中,我想创建一个显示原始视图名称的新视图。新的观点应该是可以单独处理的 var ypsilon = 30; // go through all Element

我有点困惑

基本上,framerjs将我的photoshop文档导出到HTML5网站。所有元素似乎都在PSD对象中。我可以通过PSD[元素名称]处理我的文档中的每个项目。Framer调用这些视图。因此,可以通过myNewView=newview创建一个新视图;视图可以包含文本、图像等。到目前为止,还不错

我正在尝试以下操作:从每个现有视图中,我想创建一个显示原始视图名称的新视图。新的观点应该是可以单独处理的

var ypsilon = 30;

// go through all Elements in PSD
for (var layerGroupName in PSD) {
var newView = PSD[layerGroupName].name + "_new";
var newViewName = PSD[layerGroupName].name + "_new";

//create the new view
newView = new ImageView({x:20, y:ypsilon, width:400, height:30});

// name the new view
newView.name = newViewName;

// style for the new view
newView.style = {
            "font-family": "Arial",
            "font-size": "18px",
            "font-weight": "bold",
            "text-align": "left",
            "color": "white"
            }
// show the new view
newView.html = newViewName;

// position the next view y+30px
ypsilon = ypsilon + 30;
}


这就是我所拥有的,我知道为什么这是错误的。我能看到所有的新观点,但我不能用它们的名字来称呼它们。我只能回答newView。我知道在这种情况下for循环是错误的,所以我总是覆盖newView,而不是得到layerGroupName\u new之类的内容。我还没有成功地使用.lengh,这是我使用for循环的想法。因此,我现在陷入了困境,如果你们中的一些人能帮助我,我将非常高兴。

如果您不需要直接按名称访问视图,您可以将它们全部放在一个数组中并对其进行迭代。另一种方法是将它们存储在一个对象上,使用名称作为键,这将允许您专门通过名称访问它们

两种方法的演示:

var ypsilon = 30;

// Using an array
var views = [];

// Using an object
var viewDictionary = {};

// go through all Elements in PSD
for (var layerGroupName in PSD) {
    var newView = PSD[layerGroupName].name + "_new";
    var newViewName = PSD[layerGroupName].name + "_new";

    //create the new view
    newView = new ImageView({x:20, y:ypsilon, width:400, height:30});

    // name the new view
    newView.name = newViewName;

    // style for the new view
    newView.style = {
                "font-family": "Arial",
                "font-size": "18px",
                "font-weight": "bold",
                "text-align": "left",
                "color": "white"
                }
    // show the new view
    newView.html = newViewName;

    // position the next view y+30px
    ypsilon = ypsilon + 30;

    views.push(newView);

    viewDictionary[newViewName] = newView;
}

// Looping over all the views and acting on them
for (var i = 0, view; i < views.length; i ++) {
    view = views[i];
    // do stuff with view
}

// Accessing a specific view by name via the viewDictionary
var view = viewDictionary['myViewName_new'];

这太棒了。我了解阵列的版本。现在我可以执行视图[0].onclick,functionevent{views[0].x=100}非常好。正如我好奇的那样:这将如何处理viewDictionary对象。这将是很好的名称地址。例如,一个原始视图被称为Background,新视图是Background\u new。viewDictionary['Background_new'].x=100给我一个错误。你能帮我把事情弄清楚吗?还是我说的全是胡说八道?你不会胡说八道的:你试图查字典的方式是正确的。再次检查是否使用访问该视图时使用的键将视图添加到对象中。