As other participants pointed out: the first assignment assigns a new link to an existing object, the second executes a shallow copy.
For small things, I mean: only the base object will be copied, there is no recursion
var a = {some:'propery', another:{might:'be', an: 'object, too'} }; var b = {}; for(var p in a) { b[p] = a[p]; } b.some = 'b\ own property'; console.log(a.some);
To solve this problem, you will be forgiven to think that a simple recursive function is enough:
var cloneObj = function(o) { var p,r = {}; for (p in o) {
But tire of circular links!
This will result in infinite recursion, such as a deadlock scenario, unless you add validation only for such cases:
var cloneObj = function(o) { var p,r = {}; for (p in o) {
Now this is pretty verbose, and in most cases, exhaustive search, if all you need is two objects with the same data, but they can be changed independently (i.e. do not refer to the same object in memory), everything, what you need 1 line of code:
var a = {some:'propery', another:{might:'be', an: 'object, too'} }; var b = JSON.parse(JSON.stringify(a));
Bottom line: link assignment is certainly more efficient: it does not require the constructor of the object to be called a second time, nor does it require an additional copy of any of the constants.
Disadvantage: you fall into one object and can inadvertently change / delete what you think still exists when you use another link ( delete b.some;/*some time later*/a.some.replace(/p/g,'q');//<--error )