0
是否有方法僅克隆指定屬性中的對象。新對象只能包含數組中指定的屬性僅克隆具有給定屬性的對象
是否有方法僅克隆指定屬性中的對象。新對象只能包含數組中指定的屬性僅克隆具有給定屬性的對象
function cloneWithProperties(obj, props) {
let retObj = {};
if (!props) { // if no props are specified
throw new Error('Props must be specified');
}
props.forEach(function(element) {
retObj[element] = obj[element];
}, this);
return retObj;
}
let old = {
name: 'clinton',
age: 10,
salary: 5000
}
let n = cloneWithProperties(old, ['name']);
console.log(n); // { name: 'clinton' }