jrburke/requirejs — Issue #204: Strange behavior during module loading
Author: guidocalvano
Created: 2012-03-06 18:37:47+00:00
State: open

I have the following code:

// declare.js

define( [], function()
{ function MyPrototypeFunction() {} ; return MyPrototypeFunction ; } ) ;

// declareB.js

define( [], function()
{ function YourPrototypeFunction() {} ; return YourPrototypeFunction ; } ) ;

// define.js

define( ['./declare.js', './declareB.js' ],
function ( MyPrototypeFunction, YourPrototypeFunction )
{
MyPrototypeFunction.prototype.init = function( y )
{
if( y ) this.bla = ( new YourPrototypeFunction() ).init( false ) ;

return this ;
} ;

return MyPrototypeFunction ;
} ) ;

// defineB.js

define( ['./declareB.js', './declare.js' ],
function ( YourPrototypeFunction, MyPrototypeFunction )
{
YourPrototypeFunction.prototype.init = function( x )
{
if( x ) this.bla = ( MyPrototypeFunction() ).init( true ) ;
return this ;
} ;

return YourPrototypeFunction ;
} ) ;

// try.js

define( [ './declare.js', './define.js', './declareB.js', './defineB.js' ], function( Declare, Define, DeclareB, DefineB )
{
if( Declare === Define ) console.log( 'equal' ) ; else console.log( 'unequal' ) ;

if( DeclareB === DefineB ) console.log( 'equalB' ) ;
else console.log( 'unequalB' ) ;

// any of the following examples fails:
var x = ( new DefineB() ).init( true ) ;
// var x = ( new DeclareB() ).init( true ) ;

// var x = ( new Declare() ).init( true ) ;
// var x = ( new Define() ).init( true ) ;

} ) ;

// m.js

require( 'try.js' ) ;

I run using

node r.js m.js

With result:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot call method 'init' of undefined
at YourPrototypeFunction.init (/Volumes/Macintosh2/editor/defineB.js:6:55)
at /Volumes/Macintosh2/editor/try.js:10:27
at Function.execCb (/Volumes/Macintosh2/editor/r.js:1871:25)
at execManager (/Volumes/Macintosh2/editor/r.js:540:31)
at /Volumes/Macintosh2/editor/r.js:641:25
at execManager (/Volumes/Macintosh2/editor/r.js:618:17)
at main (/Volumes/Macintosh2/editor/r.js:943:17)
at callDefMain (/Volumes/Macintosh2/editor/r.js:954:18)
at Object.completeLoad (/Volumes/Macintosh2/editor/r.js:1529:21)
at Function.load (/Volumes/Macintosh2/editor/r.js:2285:17)
Macintosh-78:editor guidocalvano$

Help would be welcome...

And a big thanks in advance.