Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Created February 22, 2020 03:40
Show Gist options
  • Save brainysmurf/6079f53b86b0d6e04b49c8589a02d6e5 to your computer and use it in GitHub Desktop.
Save brainysmurf/6079f53b86b0d6e04b49c8589a02d6e5 to your computer and use it in GitHub Desktop.
'use strict' and Google AppsScript with V8 Runtime

Google V8 Engine and 'use strict'

Google V8 runtime by default is not run in strict mode.

Reproduce

The Function.apply method when run in strict mode returns the global context, so we can test with the given code.

  • Execute testStrict()
  • Check the Logs where __g__ is not null

Conclusion

The Google AppsScripts runtime is run in non-strict mode, and we can get access to global scope via this method.

Discussion

I was under the impression that ES6 was run in strict mode; I was mistaken. It's ES6 modules that are run in strict mode.

function globalStrictMode_() {
'use strict';
return function () {
return this; // returns null in strict mode, global object if not
}.apply(null, []);
}
function globalDefaultMode_() {
return function () {
return this; // returns null in strict mode, global object if not
}.apply(null, []);
}
function testStrict() {
let __g__ = globalDefaultMode_();
Logger.log(`is global null? ${__g__ == null}`);
__g__ = globalStrictMode_();
Logger.log(`is global null? ${__g__ == null}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment