Javascript 需求和功能

Javascript 需求和功能,javascript,requirejs,Javascript,Requirejs,mymain.js中有一部分包含如下函数: function isTrue(x){...} function resizeEditors() {...} function updateLayout() {...} function prettify() {...} function setTheme(theme) {...} function themedLayout(isDark){...} function enablePanel(panel) {...} function disableP

my
main.js
中有一部分包含如下函数:

function isTrue(x){...}
function resizeEditors() {...}
function updateLayout() {...}
function prettify() {...}
function setTheme(theme) {...}
function themedLayout(isDark){...}
function enablePanel(panel) {...}
function disablePanel(panel) {...}
function enableDefaultPanels() {...}
function toggleFullscreen() {...}
function toggleEditorFullscreen(selected) {...}
有没有办法使这些函数对我的
main.js
文件的依赖项可用

例如,在
editors.js
中,我正在使用
isTrue
函数,但是editors.js模块当前找不到
isTrue
,因为它在
main.js
文件中

editors.setShowPrintMargin( isTrue( settings.showPrintMargin ) );

编辑

项目的外观:

define(["globals"], function(globals) {
    // ...
    setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});
main.js

require(['jquery', 'appSession', 'editors'], function ($, appSession, editors) {
    function isTrue(x){...}
    function resizeEditors() {...}
    function updateLayout() {...}
    function prettify() {...}
    function setTheme(theme) {...}
    function themedLayout(isDark){...}
    function enablePanel(panel) {...}
    function disablePanel(panel) {...}
    function enableDefaultPanels() {...}
    function toggleFullscreen() {...}
    function toggleEditorFullscreen(selected) {...}
});
define(['jquery', 'appSession'], function($, appSession){
    ...
    editors.setShowPrintMargin( isTrue( settings.showPrintMargin ) );
    ...
    return editors;
});
editors.js

require(['jquery', 'appSession', 'editors'], function ($, appSession, editors) {
    function isTrue(x){...}
    function resizeEditors() {...}
    function updateLayout() {...}
    function prettify() {...}
    function setTheme(theme) {...}
    function themedLayout(isDark){...}
    function enablePanel(panel) {...}
    function disablePanel(panel) {...}
    function enableDefaultPanels() {...}
    function toggleFullscreen() {...}
    function toggleEditorFullscreen(selected) {...}
});
define(['jquery', 'appSession'], function($, appSession){
    ...
    editors.setShowPrintMargin( isTrue( settings.showPrintMargin ) );
    ...
    return editors;
});

是的,你可以退货

define(function () {
    return {
        isTrue: function() {
            // Code
        },
        otherFunction: function() {
            // Code
        }
    }
});
那就这样用它们

require(["main"], function(main) {

    main.isTrue(false);

});

您可以在上了解有关定义模块的更多信息。

是的,您可以返回模块

define(function () {
    return {
        isTrue: function() {
            // Code
        },
        otherFunction: function() {
            // Code
        }
    }
});
那就这样用它们

require(["main"], function(main) {

    main.isTrue(false);

});

您可以在上了解有关定义模块的更多信息。

您可以创建包含共享/全局功能的模块,并使其成为需要它的模块的依赖项:

globals.js:

define([], function() {
    function isTrue(x){}
    // rest of functions...
    function toggleEditorFullscreen(selected) {}

    return { // return functions... };
});
然后使用它:

require(["globals", "editors"], function(globals, editors) {
    // ...
    editors.setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});
或者,如果您想在编辑器模块中使用它,您的editors.js将如下所示:

define(["globals"], function(globals) {
    // ...
    setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});
或者,如果你真的想让他们全球化,你应该能够做到:

window.isTrue = function(valueToCheck) {
    // implementation ...
};

您可以创建包含共享/全局功能的模块,并使其成为需要它的模块的依赖项:

globals.js:

define([], function() {
    function isTrue(x){}
    // rest of functions...
    function toggleEditorFullscreen(selected) {}

    return { // return functions... };
});
然后使用它:

require(["globals", "editors"], function(globals, editors) {
    // ...
    editors.setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});
或者,如果您想在编辑器模块中使用它,您的editors.js将如下所示:

define(["globals"], function(globals) {
    // ...
    setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});
或者,如果你真的想让他们全球化,你应该能够做到:

window.isTrue = function(valueToCheck) {
    // implementation ...
};

请向我们展示您当前的依赖关系图,以及您如何使用require.js请向我们展示您当前的依赖关系图,以及您如何使用require.js是否有方法将它们注入到窗口而不是main中?这样我就可以用
isTrue()
而不是
main.isTrue()
调用它们。这是个坏主意,它会污染全局命名空间。但是是的,您可以像这样将其添加到窗口
window.badFunction=function(){//this is bad}
。然而,当您使用requirejs时,这样做是没有意义的。有没有办法将它们注入窗口而不是主窗口?这样我就可以用
isTrue()
而不是
main.isTrue()
调用它们。这是个坏主意,它会污染全局命名空间。但是是的,您可以像这样将其添加到窗口
window.badFunction=function(){//this is bad}
。但是,当您使用requirejs时,这样做是没有意义的。您可以在任何文件中这样做。无论何时包含/执行文件/模块,函数都会添加到窗口中,因此全局可用。您可以在任何文件中执行。无论何时包含/执行文件/模块,函数都会添加到窗口中,因此全局可用。