second
is in fact a new object, with first
being the prototype of second
. The reason why
second.test = 20;
does not work is because upon assignment, it will look for the settings on the prototype (i.e. configurable
, enumerable
, writable
, [[Extensible]]
) and not assign to the instance if any of these are false1. To assign directly to the instance, you'll have to use Object.defineProperty
on second
:
var first = {
test: 10
};
Object.freeze(first);
var second = Object.create(first);
Object.defineProperty(second, 'test', { value: 20, enumerable: true, configurable: true, writable: true });
console.log(second.test); // 20