From d2dd36b4558f483682f3c672630fdcb36a96d4d2 Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Wed, 24 Jun 2015 10:30:31 -0400 Subject: [PATCH] Improve the detection of global/root object --- .jshintrc | 1 + lib/async.js | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.jshintrc b/.jshintrc index 7427dce48..c66d74c88 100644 --- a/.jshintrc +++ b/.jshintrc @@ -20,6 +20,7 @@ "browser": true, "node": true, "globals": { + "self": true, "define": true } } diff --git a/lib/async.js b/lib/async.js index 59ff57b97..41d33cb4a 100644 --- a/lib/async.js +++ b/lib/async.js @@ -11,17 +11,14 @@ function noop() {} // global on the server, window in the browser - var root, previous_async; - - if (typeof window == 'object' && this === window) { - root = window; - } - else if (typeof global == 'object' && this === global) { - root = global; - } - else { - root = this; - } + var previous_async; + + // Establish the root object, `window` (`self`) in the browser, `global` + // on the server, or `this` in some virtual machines. We use `self` + // instead of `window` for `WebWorker` support. + var root = typeof self === 'object' && self.self === self && self || + typeof global === 'object' && global.global === global && global || + this; if (root != null) { previous_async = root.async;