Here is a user-defined mixin for shallow cloning of objects:
function mixin(receiver, supplier) {
Object.keys(supplier).forEach(function(key) {
receiver[key] = supplier[key];
});
return receiver;
}
var supplier = {
a:{b:10}
};
var receiver = mixin({},supplier);
and according to my understanding assigning one object to other makes them equal since they have reference to same object literal and when one object changes its property it reflects on the other object too but the below tests puzzled me:
receiver.a === supplier.a //true
receiver.a = {b:20} //but
supplier.a //still {b:10} I expected {b:20}
What am I doing wrong here?? P.S I know about ES6 Object.assign() but this mixin is created just for my understanding.