要求 JS 回调参数未定义

Require JS callback argument undefined

首先,我是 Require JS 的新手,我还没有尽我所能阅读文档。有点像从臀部开枪。

但这是应该起作用的功能。据我所知。

我有一个散列 URL,在这个阶段说它是 #index。 然后我在 /javascript/pages/index.js.

下有等效的 js 页面

如您所想,我正在尝试加载这些页面 "dynamically"。 但是,我的回调函数页面参数未定义。

require(['javascript/pages/' + page],
    function(page) {
        var constructedPage = new page();
    });

所有页面都是"classes"function index(){}

与此同时,我将开始更多地阅读文档。

如果您想在 require() 的回调中使用在 index.js 中创建的 objects/variables/etc,您必须使用 define() 调用来指定该对象。

index.js

define(function(){

  // create an object with constructor
  function myPage(){
  }

  // some more code adding to the prototype

  // return the actual object
  return myPage;

});

然后您可以像在代码中那样使用该对象。

注意:define() 调用可能有其自身的依赖关系。为简单起见,此处省略。