1

In V8 at least, in the debugger, you see local, script and global categorizing the variables.

enter image description here

I got a reference to global. All you do for that is set this on entry to a property to use later if need be.

However, I can't find how to save a reference to the script object. I think it exists because that's what the debugger is looping through in the watch window.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
toddmo
  • 20,682
  • 14
  • 97
  • 107

1 Answers1

2

Before ES6, All declarations outside a function (and function declaration themselves) were properties of global object. After ES6, There are two kinds of global records:

  • Object record- Same as ES5.

    • Function declarations
    • Function generators
    • Variable assignments var
  • Declarative record - New

    • let, const, class, etc

Those in the declarative record are not accessible from the global "object", though they are globals themselves. They are accessible from the script, but the object/internal data structure holding the declarative records itself is not accessible or enumerable from inside the script. This declarative record is shown in v8 debugger as properties of script object.

References:

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Thank you. Nice answer. How do you think the GAS debugger is gathering all such declarations under the "script" object for the watch window? – toddmo May 21 '20 at 10:55
  • @toddmo The declarative records are accessible from the debugger. The "how" is implementation specific and probably proprietory logic, which even if we knew, cannot be replicated without reverse engineering and rebuilding a new custom v8 engine. – TheMaster May 21 '20 at 11:17