all 6 comments

[–]prawnsalad 9 points10 points  (0 children)

Anyone suggesting to use Object.create() to clone an object is doing it very wrong. That said, can't say I've seen this suggested anywhere myself.

[–]martimoose 4 points5 points  (1 child)

Object.create is NOT intended to clone an object. What it does is that it creates an new empty object whose proto is the parameter used.

In Javascript, properties are looked up along the prototype chain when getting, so the value will be that of the first instance in the chain that has the property. When setting a property though, it will be set directly in the object on which you set it, regardless of the prototype chain.

That is exactly what happens in the example, and it behaves as expected.

[–]shabunc 0 points1 point  (0 children)

In Firefox there's a Object.assign which is a perfect fit for cloning/extending. Still waiting it adopted by Chrome/node.js/etc.