Determining the Module Detection Module Volume Using jQuery

Let's say I have this module, and I want it to be initialized and attached to its area. For instance:

(function( scope ) {
    var Module = (function() {
      return {
          init: function(){
              console.log('Initialized');
          }
      };
    })();
    var module = scope.Module = Module;
    module.init();
})( self );

Now the problem is that always . I do not want it. I want it to be the scope in which it was called and loaded by jQuery , for example: self window$.getScript()

var Master = (function($) {
    return {
        init: function() { 
            var self = this;
            $.getScript("/js/libs/module.js");
        }
    }
})(jQuery)

Is there any way to hack this?

+3
source share
2 answers

I don’t think you can embed a scope in a stand-alone executable script call with $ .getScript. Instead, you should use some export variable to hold the script until the scope is entered.

(function( exports ) {
   exports.Module = function() {
     return {
        init: function(scope){
           console.log('Initialized', scope);
        }
     };
   };
   var module = exports.Module;
})( exports || window.exports = {} );

Then:

var self = this; // or whatever you want the scope to be
$.getScript("/js/libs/module.js", function(){
    exports.Module().init(self);
});

, jQuery , , , require.js Frame.js.

+3

JavaScript , . JS {} . " " jQuery, , - :

(function( scope ) {
    var Module = (function() {
      return new function() {
          this.init = function(){
              console.log('Initialized');
          }
      };
    })();

    var module = scope.Module = Module;
    module.init();

})();

, , :

(function( scope ) {
    var Module = new function() {
        this.init = function(){
          console.log('Initialized');
        };
    };

    var module = scope.Module = Module;
    module.init();

})();

- , .

0

All Articles