What is “define” in RequireJS?

给你一囗甜甜゛ 提交于 2019-12-10 12:25:53

问题


What is define in RequireJS? Is it a constructor or a function or...?

  1. Create module app. Before create module it loads module app2. After initialize callback:

    define(
        'app',
        ['app2'],
        function( app2 ){
            console.log(app2)
        }
    ); 
    
  2. What's this syntax?

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

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

回答1:


From the comments:

 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.

And its signature is:

define = function (name, deps, callback)

In javascript every function is a Function object, so in both cases you are passing to define an object.

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


来源:https://stackoverflow.com/questions/27195500/what-is-define-in-requirejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!