2011-12-02 55 views
44

我开始阅读关于RequireJS的几个教程。在他们中没有一个是为我解释令人满意的“定义”关键字。有人可以帮我:RequireJS库的定义说明

define(
    ["Models/Person", "Utils/random", "jquery"], 
    function (Person, randomUtility, $) {..} 
) 

什么是“define”?定义一个函数,其中包含一个数组和一个匿名函数?或者是别的什么?有人能给我更多关于这种定义的信息吗?

此外:谢谢你nnnnnn和pradeek你的答案。在欧洲,当我发布问题的那天晚上是2点30分。也许因此我不认为这是一个简单的函数调用。

回答

57

define不是特定于RequireJS,它是AMD specification的一部分。 Burke会注意到RequireJS并没有完全实现AMD如何指定它,因为AMD并没有真正关注浏览器。

define在其中没有匿名功能。 define是一种可用于AMD基于JavaScript的文件加载其数据的方法。像RequireJS这样的库可以让你使用它。具体的实施可能对你没有价值。所以我会回顾一下你提供的,因为它是声明模块的最常见的方式。

define([array]object);

阵列是模块的列表,该模块依赖于。模块和文件之间有1对1的关系。您不能在一个文件中包含多个模块,也不能在一个模块中包含多个文件。

对象是您正在定义的模块。这可以是任何东西,结构或返回结构的函数。请阅读RequireJS的文档了解更多详情。

如果object是一个函数,则传递给该函数的参数是作为第一个define参数中的依赖项列出的模块。当你通过一个函数object时,它也只会运行一次。在这个实例化上创建的方法或属性可以随时访问,然后可以被其他模块访问,这些模块将这个模块列为依赖项。

祝你好运,我建议你在这种情况下玩弄,并在事情没有意义时阅读文档。 RequireJS文档是AMD模块如何工作的快速入门。

1

我发现此页面Why AMD?非常有帮助。从这个页面总结,AMD规范有助于克服“用隐式依赖关系编写一堆脚本标记,你必须手动排序”问题。在执行所需功能之前加载依赖关系很有用,类似于其他编程语言(如python)中的import。 AMD还防止全球命名空间污染问题。检查"It is an improvement over the web's current "globals and script tags" because"部分。

5

我发现define定义在需求的底部附近。JS(我也想知道什么样的事情这define字,这就是答案一直在寻找):

/** 
* The function that handles definitions of modules. Differs from 
* require() in that a string for the module should be the first argument, 
* and the function to execute after dependencies are loaded should 
* return a value to define the module corresponding to the first argument's 
* name. 
*/ 
define = function (name, deps, callback) { 
    var node, context; 

    //Allow for anonymous modules 
    if (typeof name !== 'string') { 
     //Adjust args appropriately 
     callback = deps; 
     deps = name; 
     name = null; 
    } 

    //This module may not have dependencies 
    if (!isArray(deps)) { 
     callback = deps; 
     deps = null; 
    } 

    //If no name, and callback is a function, then figure out if it a 
    //CommonJS thing with dependencies. 
    if (!deps && isFunction(callback)) { 
     deps = []; 
     //Remove comments from the callback string, 
     //look for require calls, and pull them into the dependencies, 
     //but only if there are function args. 
     if (callback.length) { 
      callback 
       .toString() 
       .replace(commentRegExp, '') 
       .replace(cjsRequireRegExp, function (match, dep) { 
        deps.push(dep); 
       }); 

      //May be a CommonJS thing even without require calls, but still 
      //could use exports, and module. Avoid doing exports and module 
      //work though if it just needs require. 
      //REQUIRES the function to expect the CommonJS variables in the 
      //order listed below. 
      deps = (callback.length === 1 ? ['require'] : ['require', 'exports', 'module']).concat(deps); 
     } 
    } 

    //If in IE 6-8 and hit an anonymous define() call, do the interactive 
    //work. 
    if (useInteractive) { 
     node = currentlyAddingScript || getInteractiveScript(); 
     if (node) { 
      if (!name) { 
       name = node.getAttribute('data-requiremodule'); 
      } 
      context = contexts[node.getAttribute('data-requirecontext')]; 
     } 
    } 

    //Always save off evaluating the def call until the script onload handler. 
    //This allows multiple modules to be in a file without prematurely 
    //tracing dependencies, and allows for anonymous module support, 
    //where the module name is not known until the script onload event 
    //occurs. If no context, use the global queue, and get it processed 
    //in the onscript load callback. 
    (context ? context.defQueue : globalDefQueue).push([name, deps, callback]); 
}; 
0

我觉得RequireJs API specification概括起来相当不错:

如果模块具有依赖关系,则第一个参数应该是一个依赖项名称数组,而第二个参数应该是一个定义函数。一旦加载了所有依赖关系,该函数将被调用来定义模块。该函数应该返回一个定义该模块的对象。

他们列出了所有定义的各种语法形式的例子。