Search code examples
javascriptanonymous-functionuglifyjsgrunt-contrib-uglify

Uglify with anonymous function


I have a .js like this and i'm compacting it:

'use strict';
!(function () {
  var object = typeof exports != 'undefined' ? exports : this;
}());

When I use the Google Closure compiler I don't get any erros and the 'this' is referencing the window object. But when I use the Uglify ( with mangle true or false ) it gets a undefined on the 'this' ( so the object is undefined and I receive the error ). Does anyone knows why?

Basically I can just change the this to window and it will work but my concern is about other codes, libs or something else that I'm uglyfing in the future.

Note: I'm using the grunt-contrib-uglify plugin with the options: mangle: true, preserverComments: false, sourceMap: true


Solution

  • It is expected behaviour:

    with 'use strict'; a function's context is undefined, not a global object.