RequireJS库的定义说明

我开始阅读有关RequireJS的几个教程。 在他们中没有一个是为我定义的“定义”关键词。 有人可以帮助我以下几点:

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

什么是“定义”? 在里面定义一个数组和一个匿名函数的函数? 或者是别的什么? 有人能给我更多关于这种定义的信息吗?

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

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

define中没有一个匿名函数。 define是一种为基于AMD的JavaScript文件提供加载数据的方法。 像RequireJS这样的库可以让你使用它。 具体的实现可能对你没有价值。 所以我会重复一遍你提供的,因为这是声明一个模块的最常用的方法。

define( [array]object );

数组是这个模块依赖的模块列表。 模块和文件之间有一对一的关系。 一个文件中不能有多个模块,一个模块也不能有多个文件。

对象是你正在定义的模块。 这可以是返回结构的任何东西,结构体或函数。 阅读有关RequireJS的文档以获取更多详细信息。

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

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

我发现在require.js底部附近define定义(我也想知道这个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]); }; 

我发现这个网页为什么AMD? 很有帮助。 从这个页面总结,ADM规范有助于克服“用隐式依赖关系编写一堆脚本标记,你必须手动sorting”问题。 在执行所需的函数之前加载依赖项是很有帮助的,类似于像Python这样的其他编程语言的import 。 此外,该模块可以防止全局命名空间污染问题。 检查"It is an improvement over the web's current "globals and script tags" because"部分。

我认为RequireJs API规范总结得非常好:

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

他们列举了所有定义的各种语法forms的例子。