user310291
user310291

Reputation: 38190

How to split javascript object definition in several files

Let's say I have an Object View original defined in one file View.js :

      var View = (function () {

      function View() {
      }

       View.prototype.SubFunc = function() {

      }   

      return View;
  })();

Now I want to be able to split in 2 files :

View.js

  var View = (function () {

      function View() {
      }

      return View;
  })();

and SubFunc.js

      View.prototype.SubFunc = function() {

      }   

Why it doesn't seem to work anymore ? During execution I get this error :

      view.SubFunc is not a function

Upvotes: 1

Views: 52

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

JS files are imported one by one but asynchronously and there is no guarantee that file1 will be loaded before file2.

You can wrap file2 (SubFunc.js) into a document.onload event so that it will be executed when other files are loaded.

document.onload = function(){

   View.prototype.SubFunc = function() {
   }   
}

Upvotes: 1

Related Questions