Javascript forward declaration

Background

I am creating a javascript based application that works differently on mobile and desktop devices. However, with the exception of DOM manipulation, most of the codes are distributed between both platforms, so I structured all the files, such as: * foo.core.js * foo.mobile.js * foo.web.js

And hope to use object-oriented methods to write cleaner code.

Problem:

I have two JavaScript files, with classes

File 1:

function ClassA()
{}

ClassA.prototype.foo = function(){};

GreatGrandChildA.prototype = new GrandChildA(); // this is where the error is
function GreatGrandChildA ()
{}

File 2:

ChildA.prototype = new ClassA();
function ChildA () // ChildA inherits ClassA
{}

GrandChildA.prototype = new ChildA()
function GrandChildA () // GrandChildA inherits ClassA
{}

Typically, in a language like C ++, I would redirect the declaration GrandChildAdirectly to file 1. I would like to know how to do this in Javascript

Edit:

If I create a single file containing all four classes - in the same order in which they are loaded, the example works exactly as expected:

http://jsfiddle.net/k2XKL/

+5
4

js:

File1:

// ClassB: inherite from ClassA
(function ClassB_Builder() {
  if(window.ClassB)return; // ClassB is already defined;
  if(!window.ClassA) { // ClassA is already not defined;
     setTimeout(ClassB_Builder,0); // shedule class building
     return;
  }
  ClassB=function() {
  }
  ClassB.prototype=new ClassA;
  ClassB.prototype.constructor=ClassB; // can be important for inheritance!!!
})();

File2:

// ClassA: base class
(function ClassA_Builder() {
  ClassA=function() {
  }
})();

// ClassC: inherite from ClassB
(function ClassC_Builder() {
  if(window.ClassC)return; // ClassC is already defined;
  if(!window.ClassB) { // ClassB is already not defined;
     setTimeout(ClassC_Builder,0); // shedule class building
     return;
  }
  ClassC=function() {
  }
  ClassC.prototype=new ClassB;
  ClassC.prototype.constructor=ClassC; // can be important for inheritance!!!
})();
+3

, HTML 1, 2.

1 , "GrandChildA" - undefined. , 2 .

2 :

ChildA.prototype = new ClassA();
function ChildA () // ChildA inherits ClassA
{}

Javacript "ClassA" , ChildA.prototype = new ClassA();

, , http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

+1

, , - 2 . , , script (python script), ( , ) ( ). , gzipping. .

+1

, . , , - . - , .

, ++ . . ++, , . , .

0