3

Say I created an object like this

var obj = {M:"soober"};

its [[Prototype]] internal property points to Object.prototype.

Now, if I run...

Object.seal(Object.prototype);

...does this make all objects in the program immutable?

I need some clarity in this matter.

1
  • no, seal/freeze is shallow... Commented Oct 13, 2015 at 4:52

4 Answers 4

1

Lets see:

> var b = Object.create(Object.seal({}));
> b.foo = 'bar';
> b.foo
  "bar"

...does this make all objects in the program immutable?

No.

From the MDN documentation:

The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

Since b is not sealed (its prototype is), there is no problem adding new properties to it.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. For immutability you need Object.freeze, not Object.seal
  2. Freezing a prototype only prevents the prototype instance itself from being mutated. It does not affect ancestors, which may have internal state that change the frozen object's behavior. Similarly descendants are not frozen, they merely face restrictions on defining properties that exist on the frozen prototype (similar to non-configurable properties). And even that restriction can be hacked around (don't do that). And its ancestors might also be stateful

For such simple things I would recommend simply trying them out on the console and/or reading the docs

4 Comments

Point 2 seems to be wrong: descendants can not set properties normally if the prototype is frozen. Give it a try.
@trusktr, (() => {let prot = Object.create(null); let instance = Object.create(prot); Object.freeze(prot); instance.foo = "bar"; console.log(instance)})();. descendant can be modified with a frozen prototype.
Ah, looks like it only affects properties that pre-exist in the prototype. Try this one: { let a = Object.freeze({foo: 'foo'}); let b = Object.create(a); b.foo = 'bar'; console.log(b); }.
@trusktr sure, but that's also the case with non-configurable on the prototype, so that's nothing new. but you are right in so far that "has no effect" is an overly broad statement, i'll correct my answer.
0

The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.

Comments

0

The prototype is a separate object, whose properties are only queried if no such property is found in the object itself (and so on, down the prototype chain).

Therefore sealing the prototype only prevents new properties from being added to the prototype itself, and has no effect on whether properties can be added to other objects that inherit from that prototype.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.