0

Since ES5 we can use Object.freeze() to lock objects.

But if you run JS in sloppy-mode any writing to the properties fails silently, unless you activate strict-mode.

But where do you need to set this? I use a module system (node.js/amd) so there are a bunch of files/scopes involved:

  1. Scope where object was created originally.
  2. Scope where the Object.freeze() was called on the object.
  3. Scope where an attempt is made to change the object's properties.
  4. Global scope
  5. ???

Which one of these MUST have (inherit) a 'use strict' directive to trigger the error?

Bartvds
  • 3,340
  • 5
  • 30
  • 42

1 Answers1

2

Scope where object was created originally.

No. That doesn't matter.

Scope where the Object.freeze() was called on the object.

That's only relevant when freeze would throw.

Scope where an attempt is made to change the object's properties.

Yes. [[Put]] does throw when the evaluated reference is strict, and the reference is constructed by property accessors as follows:

7. If the syntactic production that is being evaluated is contained in strict mode code, let strict be true, else let strict be false.

8. Return a value of type Reference whose base value is baseValue and whose referenced name is propertyNameString, and whose strict mode flag is strict.

Global scope

No. You cannot make "global scope" strict. Strictness is a property of program code, and the highest-level unit of that is "Program code" - in your case the module file.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375