51

我开始阅读一些关于 RequireJS 的教程。其中没有一个对我来说令人满意地解释了“定义”关键字。有人可以帮我解决以下问题:

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

什么是“定义”?是否定义了一个包含数组和匿名函数的函数?或者是别的什么?有人可以给我更多关于这种定义的信息吗?

补充:谢谢 nnnnnn 和 pradeek 的回答。在欧洲,当我发布问题时是晚上 2:30。也许因此我没有意识到这是一个简单的函数调用。

4

4 回答 4

62

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

define里面没有匿名函数。 define是一种可供基于 AMD 的 JavaScript 文件用于加载其数据的方法。像 RequireJS 这样的库让你可以使用它。具体的实现可能对您没有价值。因此,我将介绍您提供的那个,因为它是声明模块的最常见方式。

define( [array],object );

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

Object 是您正在定义的模块。这可以是任何东西、结构或返回结构的函数。阅读RequireJS上的文档以获取更多详细信息。

如果 object 是一个函数,则传递给函数的参数是第一个定义参数中列为依赖项的模块。同样重要的是要注意,当您将函数传递为 时object,它只会运行一次。但是,可以随时访问在此实例化上创建的方法或属性,然后可以由将此模块列为依赖项的其他模块访问。

祝你好运,我建议在事情没有意义时尝试一下并阅读文档。RequireJS 文档非常适合作为 AMD 模块如何工作的快速入门。

于 2011-12-03T02:31:10.327 回答
5

我在definerequire.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]);
};
于 2013-11-11T18:52:15.817 回答
1

我找到了这个页面为什么是 AMD?很有帮助。从这个页面总结,AMD 规范有助于克服“编写一堆带有隐含依赖关系的脚本标签,你必须手动订购”的问题。它有助于在执行所需函数之前加载依赖项,类似于importpython 等其他编程语言。AMD 还可以防止全局命名空间污染问题。检查"It is an improvement over the web's current "globals and script tags" because"部分。

于 2013-08-28T13:18:00.667 回答
0

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

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

它们列出了定义的所有各种句法形式的示例。

于 2015-06-16T00:52:47.057 回答