di.js icon indicating copy to clipboard operation
di.js copied to clipboard

Invalid assumption of a class to be a factory

Open tusharmath opened this issue 11 years ago • 5 comments

Overview Uglify converts all class names to single letter lower case ones the factory provider automatically get created instead of the class provider. This should help rectify it.

Reproducable: always Browsers: Chrome 39.0.2171.95 Operating System: OS X Steps to reproduce

  • Create two classes inside a closure
(function () {
    var di = require('di');

    function Apples(b) {
        this.bapples = b;
    }

    Apples.prototype.print = function () {
        console.log(this.bapples.getMyName());
    };

    function Bapples() {}
    Bapples.prototype.getMyName = function () {
        return 'My Name is TUshar';
    };

    di.annotate(Apples, new di.Inject(Bapples));

    injector = new di.Injector();
    a = injector.get(Apples);
    a.print();
})();
  • run the code - works perfectly fine
  • minify the source
! function () {
    var e = require("di"),
        Injector = e.Injector;

    function t() {
        // this.bapples = t
    }
    injector = new Injector();
    a = injector.get(t);
    a.print();
}();
  • run the code (minified version), throws the following error -
    a.print();
      ^
TypeError: Cannot call method 'print' of undefined
    at /Users/tusharmathur/Documents/Projects/Executables/JavaScript.min.js:11:4
    at Object.<anonymous> (/Users/tusharmathur/Documents/Projects/Executables/JavaScript.min.js:12:2)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

Review on Reviewable

tusharmath avatar Dec 25 '14 19:12 tusharmath

@vojtajina Not sure why the build is failing. I see that it doesn't even work in master. Would it be possible to merge without the build passing? This issue is not letting my patch go to production :(

tusharmath avatar Dec 27 '14 05:12 tusharmath

Indeed seeing this issue.

iammerrick avatar Jan 07 '15 06:01 iammerrick

@vojtajina Can you please comment if this can be merged?

tusharmath avatar Jan 31 '15 15:01 tusharmath

I don't know that having prototype methods means something is a class. I think we need to be more explicit.

iammerrick avatar Jan 31 '15 23:01 iammerrick

I totally agree. But this logic is already being used to differentiate between classes and function . This pull request only refines the hack for the time being so that people who are using it in production don't face issues

tusharmath avatar Feb 01 '15 03:02 tusharmath