Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#12280 closed defect (fixed)

define() has different result with equivalent object/function argument

Reported by: tomc974 Owned by: Rawld Gill
Priority: high Milestone: 1.6
Component: Core Version: 1.6.0b1
Keywords: Cc:
Blocked By: Blocking:


I'm not sure whether this is normal behavior or a bug but the following samples produce different results (actually version is 1.6.0b2) :

define({i : 5}) ; // define custom.module
dojo.require("custom.module").i ; // can access i

define(function() { return {i : 5} ; }) ; // define custom.module
dojo.require("custom.module").i ; // Error : i is undefined

This is because when passed a function without dependencies, define adds dependencies for ["require", "exports", "module"].

At some point in loader.js the following code is executed if the argument is a function :

    dojo._loadedModules[dottedName] = returned;
    dojo.setObject(dottedName, returned);
    dojo._loadedModules[dottedName] = module.exports;

The second if is actually overridding the returned value that was stored in dojo._loadedModules which breaks dojo.require() in turn.

Change History (5)

comment:1 Changed 11 years ago by bill

Component: GeneralCore
Owner: changed from anonymous to Rawld Gill

comment:2 Changed 11 years ago by Rawld Gill

Status: newassigned

comment:3 Changed 11 years ago by Rawld Gill

Resolution: fixed
Status: assignedclosed

(In [23827]) fixes #12280

comment:4 Changed 11 years ago by Rawld Gill

Thanks tomc974 for the find!

This is an ambiguous area in the AMD spec: "If omitted, it should default to ["require", "exports", "module"]. However, if the factory function's arity (length property) is less than 3, than the loader may choose to only call the factory with the number of arguments corresponding to the function's arity or length." See

That said, I went ahead and changed the impl to inspect the factory function arity and send deps according.

Test added to show original bug and fix.

comment:5 Changed 11 years ago by bill

Milestone: tbd1.6
Note: See TracTickets for help on using tickets.