Skip to content

Commit

Permalink
Favor window global over self global
Browse files Browse the repository at this point in the history
So that Q can be used as a <script> in Firefox add-ons.
The self object does not refer to window and is immutable.

Fixes kriskowal#631
  • Loading branch information
kriskowal committed May 17, 2015
1 parent 05e20dc commit 22bd310
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@
}

// <script>
} else if (typeof self !== "undefined") {
} else if (typeof window !== "undefined" || typeof self !== "undefined") {
// Prefer window over self for add-on scripts. Use self for
// non-windowed contexts.
var global = typeof window !== "undefined" ? window : self;

// Get the `window` object, save the previous Q global
// and initialize Q as a global.
var previousQ = self.Q;
self.Q = definition();
var previousQ = global.Q;
global.Q = definition();

// Add a noConflict function so Q can be removed from the
// global namespace.
self.Q.noConflict = function () {
self.Q = previousQ;
global.Q.noConflict = function () {
global.Q = previousQ;
return this;
};

Expand Down

0 comments on commit 22bd310

Please sign in to comment.