0

TLDR: Why can't I do:

let x = Object.freeze(window)
VM431:1 Uncaught TypeError: Cannot freeze
    at Function.freeze (<anonymous>)
    at <anonymous>:1:16

It's an object, isn't it?

typeof(window)
"object"

I know there are other Qs that deal with sandboxing JS, but this question is really just trying to understand better why freezing window is not allowed.

Gillespie
  • 5,780
  • 3
  • 32
  • 54
  • [Is It Possible to Sandbox JavaScript Running In the Browser?](https://stackoverflow.com/q/195149) – VLAZ Mar 13 '21 at 22:43
  • I have read that post. This question isn't so much "how do I sandbox" but more trying to learn why `Object.freeze(window)` is not allowed. I'll edit my question accordingly. – Gillespie Mar 13 '21 at 23:20
  • `Object.freeze()` does what it says. .Likely because it makes no sense to make the whole window read-only? So where the information about frozen / sealed objects is being maintained? You can't do that, because you can't do that ... – Martin Zeitler Mar 13 '21 at 23:39
  • I also believe it is because `window` is readonly. You cannot assign to it like `window = 'a'` hence you can't also change it's attributes – vixalien Mar 14 '21 at 00:21
  • Not all attributes on window are writable. –  Mar 14 '21 at 01:14

1 Answers1

3

because it is declared to be extensible, in fact you can create such freeze-less objects on your own like this:

const someObject = new Proxy({}, {
  preventExtensions: () => {
    throw TypeError('Cannot freeze');
  }
});

someObject.x = 1;

console.log(typeof someObject);
console.log(someObject);

Object.freeze(someObject);

more about this you can read here: Proxy.

the next question may arise, why it is "made" extensible, for this we have the definition of window Global object, and this example:

var foo = "foobar";
foo === window.foo; // Returns: true

considering this, one may now wonder what would happen if we could freeze it

n--
  • 3,563
  • 4
  • 9
  • It is different. While trying to freeze `someObject`, Firefox says: `Uncaught TypeError: Cannot freeze` while when I try to freeze window Firefox says `Uncaught TypeError: can't prevent extensions on this proxy object` – vixalien Mar 14 '21 at 00:23
  • just put that message here `throw TypeError('Cannot freeze')` and you'll get the same – n-- Mar 14 '21 at 00:26
  • that is not related. I mean that freezing a `canEvolve = true` object is not the same as freezing the `window` object. Because otherwise they would throw the same error. – vixalien Mar 14 '21 at 00:33
  • error messages are not standardized by the specs, they are an implementation detail, so you may note different error between browsers when trying to freeze the `window` object, since the implementation of `window` object is browser specific – n-- Mar 14 '21 at 10:16
  • They are not standard, but they are well-known. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_define_property_object_not_extensible) I believe there is an internal mechanism that do just that but for `window` and other native objects only. – vixalien Mar 14 '21 at 21:12