Wol*_*mec 48 javascript requirejs
我开始阅读有关RequireJS的几个教程.在他们中没有一个是"define"关键字为我解释得令人满意.有人可以帮我解决以下问题:
define(
["Models/Person", "Utils/random", "jquery"],
function (Person, randomUtility, $) {..}
)
Run Code Online (Sandbox Code Playgroud)
什么是"定义"?是否定义了一个带有数组和内部匿名函数的函数?或者是别的什么?有人能给我更多关于这种定义的信息吗?
另外:谢谢nnnnnn和pradeek的回答.在欧洲,当我发布问题的那天晚上2:30.也许因此我没有意识到这是一个简单的函数调用.
Dre*_*rew 61
define
它不是RequireJS特有的,它是AMD规范的一部分.Burke会注意到RequireJS并没有完全按照AMD的规定实现它,因为AMD并没有真正考虑到浏览器.
define
它没有匿名功能. define
是一种基于AMD的JavaScript文件可用于加载其数据的方法.像RequireJS这样的库让你可以使用它.具体实施可能对您没有价值.因此,我将介绍您提供的那个,因为它是声明模块的最常用方式.
define(
[array]
, object
);
Array是此模块所依赖的模块列表.模块和文件之间存在1对1的关系.您不能在文件中包含多个模块,也不能在一个模块中包含多个文件.
对象是您定义的模块.这可以是任何东西,结构或返回结构的函数.阅读有关RequireJS的文档以获取更多详细信息.
如果object是函数,则传递给函数的参数是在第一个define参数中列为依赖项的模块.值得注意的是,与传递函数时相比object
,它只会运行一次.可以随时访问在此一个实例化上创建的方法或属性,然后可以将列出此模块的其他模块作为依赖项访问.
祝你好运,我建议玩这个,并在事情没有意义的时候阅读文档.RequireJS文档非常适合作为AMD模块工作方式的快速入门.
我发现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]);
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27514 次 |
最近记录: |