【问题标题】:Explanation of define of the RequireJS libraryRequireJS 库的定义说明
【发布时间】:2012-01-11 03:36:13
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: javascript requirejs


    【解决方案1】:

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

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

    define([array],object);

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

    Object 是您定义的模块。这可以是任何东西、结构或返回结构的函数。阅读RequireJS 上的文档了解更多详情。

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

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

    【讨论】:

      【解决方案2】:

      我发现 define 定义在 require.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]);
      };
      

      【讨论】:

        【解决方案3】:

        我发现这个页面Why AMD? 很有帮助。总而言之,AMD 规范有助于克服“编写一堆具有必须手动排序的隐式依赖的脚本标签”问题。它有助于在执行所需函数之前加载依赖项,类似于 python 等其他编程语言中的import。 AMD 还可以防止全局命名空间污染问题。检查"It is an improvement over the web's current "globals and script tags" because"部分。

        【讨论】:

          【解决方案4】:

          我认为RequireJs API specification 总结得很好:

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

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

          【讨论】:

            猜你喜欢
            • 2011-05-04
            • 2017-06-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-29
            • 1970-01-01
            • 2016-01-21
            相关资源
            最近更新 更多