Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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-定义vs要求_Javascript_Requirejs - Fatal编程技术网

Javascript RequireJs-定义vs要求

Javascript RequireJs-定义vs要求,javascript,requirejs,Javascript,Requirejs,对于模块,我不返回我一直使用的对象,而是使用require而不是define。例如,假设我有以下jQuery插件(jQuery.my plugin.js): 现在,如果我在另一个模块中说: require(['jquery', 'jquery.my-plugin'], function($) { $('#element').myPlugin(); }); 我发现这不起作用,因为我的插件还没有注册。但是,如果我在jquery.my-plugin模块中将require更改为define,那

对于模块,我不返回我一直使用的对象,而是使用require而不是define。例如,假设我有以下jQuery插件(jQuery.my plugin.js):

现在,如果我在另一个模块中说:

require(['jquery', 'jquery.my-plugin'], function($) {
    $('#element').myPlugin();
});
我发现这不起作用,因为我的插件还没有注册。但是,如果我在jquery.my-plugin模块中将require更改为define,那么它就可以正常工作


如果有人能澄清我为什么要这样做,我将不胜感激。我喜欢在开始使用之前完全理解一些东西。感谢

基本上,当您使用
require
时,您是在说“我想要这个,但我也想要它的所有依赖项”。因此,在下面的示例中,我们需要一个,但require将搜索所有依赖项,并确保在继续之前加载它们

require(['a'], function(a) {
    // b, c, d, e will be loaded
});

// File A
define(['b','c','d','e'], function() {
    return this;
});

一般的经验法则是,当您想要定义一个将被应用程序重用的模块时,您可以使用
define
,并使用
require
简单地加载依赖项。

下面是jquery.my-plugin.js中应该包含的代码,它定义了一个名为“jquery.my-plugin”的模块,可以用作依赖项其他地方

define(['jquery'], function($) { //jquery is a dependency to the jquery.my-plugin module
    $.fn.myPlugin = function(options) { //adds a function to the *global* jQuery object, $ (global since jQuery does not follow AMD)
        ...
    };
});
下面是一段代码,您希望将插件函数附加到全局jQuery对象,然后使用它

require(['jquery.my-plugin'], function() { // jquery.my-plugin is loaded which attaches the plugin to the global JQuery object as shown above, then this function fires

    //the only reason $ is visible here is because it's global. If it was a module, you would need to include it as a dependency in the above require statement
    $('#element').myPlugin(); //the $ refers to the global object that has the plugin attached
});

谢谢,但是为什么我不能使用require而不是define来定义文件?从本质上讲,它也会这样做,即文件A需要b、c、d和e才能执行。感谢您的澄清。非常感谢。@PaulOsborne:听起来类似于:“如果您有层次结构的东西,请使用类定义(
module.exports
),如果您有通用的可重用UTIL,请使用通用模块对象(
exports
)。也就是说,我们是否总是一起使用
定义
和通用模块对象,而
要求同时使用
和类定义?在这种情况下,OP中的第二个'require'语句应该是'define',不需要列出'jquery'依赖项,因为'jquery'已经是'jquery.my plugin'@nikobelic no'的依赖项。我原始问题中的第一个'require'语句应该使用'define',因为它是一个依赖项(此答案与依赖项“文件A”相反)。您是正确的,您不需要重复对“jquery”的依赖。但是,我觉得一切都很好。是的,$object是全局定义的,因此我想是否将其包含在require语句中是可选的。这样做可以避免库冲突的潜在问题,并且与define语句abo一致可能重复的
require(['jquery.my-plugin'], function() { // jquery.my-plugin is loaded which attaches the plugin to the global JQuery object as shown above, then this function fires

    //the only reason $ is visible here is because it's global. If it was a module, you would need to include it as a dependency in the above require statement
    $('#element').myPlugin(); //the $ refers to the global object that has the plugin attached
});