diff --git a/lib/browser.js b/lib/browser.js index 60dbbb99..8f9be9b3 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -10,7 +10,7 @@ options.bare = true; return Function(CoffeeScript.compile(code, options))(); }; - if (!(typeof window !== "undefined" && window !== null)) { + if (typeof window == "undefined" || window === null) { return; } CoffeeScript.load = function(url, options) { diff --git a/lib/nodes.js b/lib/nodes.js index 4a09f7df..2396f52c 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1459,10 +1459,21 @@ })(); __extends(Existence, Base); Existence.prototype.children = ['expression']; + Existence.prototype.invert = function() { + this.negated = !this.negated; + return this; + }; Existence.prototype.compileNode = function(o) { - var code; + var code, sym; code = this.expression.compile(o); - code = IDENTIFIER.test(code) && !o.scope.check(code) ? "typeof " + code + " !== \"undefined\" && " + code + " !== null" : "" + code + " != null"; + code = (function() { + if (IDENTIFIER.test(code) && !o.scope.check(code)) { + return this.negated ? "typeof " + code + " == \"undefined\" || " + code + " === null" : "typeof " + code + " != \"undefined\" && " + code + " !== null"; + } else { + sym = this.negated ? '==' : '!='; + return "" + code + " " + sym + " null"; + } + }).call(this); return o.level <= LEVEL_COND ? code : "(" + code + ")"; }; return Existence; diff --git a/src/nodes.coffee b/src/nodes.coffee index 0d160069..cd09b4a4 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1214,12 +1214,20 @@ exports.Existence = class Existence extends Base constructor: (@expression) -> + invert: -> + @negated = not @negated + this + compileNode: (o) -> code = @expression.compile o code = if IDENTIFIER.test(code) and not o.scope.check code - "typeof #{code} !== \"undefined\" && #{code} !== null" + if @negated + "typeof #{code} == \"undefined\" || #{code} === null" + else + "typeof #{code} != \"undefined\" && #{code} !== null" else - "#{code} != null" + sym = if @negated then '==' else '!=' + "#{code} #{sym} null" if o.level <= LEVEL_COND then code else "(#{code})" #### Parens