javascript RequireJs - 定义与需要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17366073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:02:46  来源:igfitidea点击:

RequireJs - Define vs Require

javascriptrequirejs

提问by nfplee

For modules I don't return an object I have been using require instead of define. For example say I have the following jQuery plugin (jquery.my-plugin.js):

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

require(['jquery'], function($) {
    $.fn.myPlugin = function(options) {
        ...
    };
});

Now if I say the following in another module:

现在,如果我在另一个模块中说以下内容:

require(['jquery', 'jquery.my-plugin'], function($) {
    $('#element').myPlugin();
});

I've found this doesn't work because myPlugin has not been registered. However if I change the require to a define within my jquery.my-plugin module then it works fine.

我发现这不起作用,因为 myPlugin 尚未注册。但是,如果我在 jquery.my-plugin 模块中将 require 更改为定义,则它可以正常工作。

I'd appreciate it if someone could clear up why I have to do this. I like to understand something fully before I go ahead and use it. Thanks

如果有人能澄清为什么我必须这样做,我将不胜感激。我喜欢在继续使用它之前完全理解它。谢谢

回答by Paul Osborne

Essentially, when you use requireyou are saying "i want this, but i want all its dependencies too". So in the example below, we're requiring A, but require will search for all dependencies and ensure they are loaded before continuing.

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

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

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

General rule of thumb is you use definewhen you want to define a module that will be reused by your application and you use requireto simply load a dependency.

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

回答by Niko Bellic

Below is the code that should be inside jquery.my-plugin.jswhich definesa module called 'jquery.my-plugin' that can be used as a dependency elsewhere.

下面是应该在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)
        ...
    };
});

Below is a section of code where you want to attach your plugin function to the global jQuery object and then use it ...

下面是一段代码,您要将插件函数附加到全局 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
});