0

在此设置中初始化对象的正确方法是什么:

for(var key in (assoc_array||{other.dict.id: other.dict})){

}

在这里我得到一个错误,部分“other.object.id”中的点不应该在那里。所以有效的方法是:

var default_object = {}
default_object[other.dict.id] = other.dict;
for(var key in (assoc_array||default_object)){
  ...
}

必须有一种方法来隐式声明默认对象吗?

4

1 回答 1

0

I dont think it is possible like that,

you may have to use a function to do it

function createObj(key, val){
    var obj = {};
    obj[key]  = val;
    return val;
}

for(var key in (assoc_array||createObj(other.dict.id, other.dict))){

}

Demo: Fiddle

于 2013-07-15T06:53:41.347 回答