Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解释RequireJS库的定义_Javascript_Requirejs - Fatal编程技术网

Javascript 解释RequireJS库的定义

Javascript 解释RequireJS库的定义,javascript,requirejs,Javascript,Requirejs,我开始阅读一些关于RequireJS的教程。其中没有一个“define”关键字对我的解释令人满意。有人能帮我做以下几件事吗 define( ["Models/Person", "Utils/random", "jquery"], function (Person, randomUtility, $) {..} ) 什么是“定义”?是否定义一个包含数组和匿名函数的函数?还是别的什么?有人能给我更多关于这类定义的信息吗 另外:感谢NNNN和pradeek的回答。在欧洲,我发帖的时候是

我开始阅读一些关于RequireJS的教程。其中没有一个“define”关键字对我的解释令人满意。有人能帮我做以下几件事吗

define(
  ["Models/Person", "Utils/random", "jquery"], 
  function (Person, randomUtility, $) {..}
)  
什么是“定义”?是否定义一个包含数组和匿名函数的函数?还是别的什么?有人能给我更多关于这类定义的信息吗


另外:感谢NNNN和pradeek的回答。在欧洲,我发帖的时候是凌晨2:30。因此,可能我没有意识到这是一个简单的函数调用。

define
不是特定于RequireJS的,它是函数调用的一部分。Burke会注意到RequireJS并没有完全实现AMD指定的方式,因为AMD并没有真正记住浏览器

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

定义(
[array]
对象

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

对象是您正在定义的模块。这可以是任何内容、结构或返回结构的函数。有关更多详细信息,请阅读上的文档

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


祝你好运,我建议你在事情没有意义的时候玩一下这个,读一下文档。RequireJS文档是AMD模块工作原理的快速入门。我发现这个页面非常有用。从本页总结,AMD规范有助于克服“编写一组具有隐式依赖项的脚本标记,您必须手动排序”问题。它有助于在执行所需函数之前加载依赖项,类似于python等其他编程语言中的
import
。AMD还防止了全球命名空间污染问题。检查
“这是对web当前的“全局和脚本标记”的改进,因为“
部分。

我在require.js的底部找到了
定义
定义(我也在想这个
定义
单词是什么东西,这就是我想要的答案):

我认为这句话概括得很好:

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

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

/**
 * 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]);
};