Search code examples
javascriptyuiminify

How do I obfuscate global variable using YUI without putting entire class inside a closure


Suppose there is a global variable which is a function

function MyClass(){}

and there are methods of this class such as

MyClass.func1 = function()
{
}

I want to ensure that YUI compression and obfuscation works without putting entire class inside a closure like

(function () {
    function MyClass(){}
    MyClass.func1 = function()
    {
    }
})();

Is there a way to make YUI compression work without doing this?


Solution

  • Well, I suppose you could wrap it in an anonymous function before compressing it, and then just remove the anonymous function after.

    Also make sure you're using prototype ;)

    (function () {
      function MyClass(){}
      MyClass.prototype.func1 = function()
      {
      }
    })();
    

    Results in:

    (function(){function a(){}a.prototype.func1=function(){}})();
    

    And just take out the anonymous function:

    function a(){}a.prototype.func1=function(){}