什么是"定义"在RequireJS?

时间:2014-11-28 20:28:35

标签: javascript requirejs

RequireJS中的define是什么?它是构造函数还是函数还是......?

  1. 创建模块应用。在创建模块之前,它会加载模块app2。初始化回调后:

    define(
        'app',
        ['app2'],
        function( app2 ){
            console.log(app2)
        }
    ); 
    
  2. 这句话是什么意思?

    define(function (require) {
        var logger = require("./app2");
        console.log(logger);
    });
    
  3. 而且:

    define({
        color: "black",
        size : "large"
    });
    

1 个答案:

答案 0 :(得分:0)

来自评论:

 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)

在javascript中,每个函数都是一个Function对象,所以在这两种情况下你都要传递给define一个对象。

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