2017-04-25 167 views

回答

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' }