如果它们不是未定义的,我想将所有属性从backbone.js 模型对象复制到另一个。这有点像孩子凌驾于父母的属性之上。子对象的某些属性应该从整个过程中排除。这是我到目前为止所拥有的:
function inherit(parent, child) {
var childAttributes = Object.keys(child.attributes);
// Don't involve these attributes in any way, they're special.
attributes = _.without(attributes, ['specialCase1', 'specialCase2', 'specialCase3']);
// This array will store undefined child attributes
var undefChildAttributes = new Array();
for (i in attributes) {
var attr = attributes[i]; // Get attribute name
var value = child.get(attr); // ... and value
var type = RealTypeOf(value); // ... and type
if (type == 'undefined') {
undefChildAttributes.push(attr);
}
}
// Update the child attributes array to not include undefined any attributes
attributes = _.without(attributes, undefChildAttributes);
// Copy any attributes from child to parent
// ------------------ PROBLEM AREA ------------------
for (var m=0; m<attributes.length; m++) {
var attr = attributes[m]; // Attribute to copy
// I want to use _.extends in some way here, maybe like:
_.extends(parent, child[attr]);
}
}
我被困在如何将属性复制到父级中。我拥有自动取款机的唯一方法是破解:
if (type == 'string') {
eval('var o = {' + attr + ':"' + child.get(attr) + '"};');
} else {
eval('var o = {' + attr + ':' + child.get(attr) + '};');
}
parent.set(o);