function copy(obj){
var copy=Object.create(Object.getPrototypeOf(obj));//创建与此匹配的对象
var propNames=Object.getOwnPropertyNames(obj);//获取所有的键(属性)
propNames.forEach(function(name){
var desc=Object.getOwnPropertyDescriptor(obj,name);//获取对象的值/或者方法
Object.defineProperty(copy,name,desc)//重新定义在copy对象上
});
return copy
}
function add(){
return this.a+this.b;
}
var obj1={a:1,b:2,add:add}
var obj2=copy(obj1);
obj2.add();//3