function Fn() {
this.x=100;
this.y=200;
this.getY=function(){
console.log(this.y);
}
}
var f1=new Fn;
var f3=null;
cloneObj(f3,f1);
console.log(f3);
function cloneObj(obj1,obj2) {
for (var key in obj2) {
if (obj2.hasOwnProperty(key)) {
console.log(obj2[key]); //100
obj1[key]=obj2[key]; //Cannot set property "x" of null
}
}
return obj1;
}
I really don"t understand. When printing, it is clear that obj2 [x] = 100, why the next line is wrong?