简体   繁体   English

如何为(子)模块同时提供node.js和浏览器支持

[英]how to provide both node.js & browser support for (sub)modules

Lets' say I've got a sample library split into 2 files: base library and additional module. 假设我有一个示例库,分为两个文件:基础库和附加模块。 The base module resides in module.js : 基本模块位于module.js

var Calculator = {
    add: function(a, b) { return a + b; },
    sub: function(a, b) { return a - b; }
};

The additional module resides in submodule.js : 附加模块位于submodule.js

if (typeof Calculator == "undefined") {                                                                                                                                                                                                                                        
        var Calculator = {};                                                                                                                                                                                                                                                   
}                                                                                                                                                                                                                                                                              

Calculator.mul = function(a, b) { return a * b; };                                                                                                                                                                                                                             

Calculator.div = function(a, b) { return a / b; };

This is how many js libraries are built (main module + submodules), though probably they're configured a lot better. 这就是构建了多少个js库(主模块+子模块),尽管它们的配置可能要好得多。 I've prepared a basic index.html file: 我已经准备了一个基本的index.html文件:

<html>
<head>
        <script type="text/javascript" src="module.js"></script>
        <script type="text/javascript" src="submodule.js"></script>
</head>
<body>
</body>
</html>

that loads the library and enables me to execute it inside the browser (console output below): 加载库并使我能够在浏览器中执行它(下面的控制台输出):

Calculator.add(3,4)
7
Calculator.sub(3,4)
-1
Calculator.mul(3,4)
12
Calculator.div(3,4)
0.75

This is all what the library does. 这就是图书馆的全部工作。 Now I want to provide support for node.js (not breaking support for browsers). 现在,我想提供对node.js的支持(不破坏对浏览器的支持)。 I've enclosed the definition within immediately-invoked-fun-expr with a root parameter, which is calculated during runtime: node's module.exports if it exists or this==window otherwise (browser). 我已经用一个根参数将定义包含在立即调用的乐趣module.exports ,该根参数是在运行时计算的:节点的module.exports如果存在),否则为this==window (浏览器)。 The code looks like this: 代码如下:

(function(root) {

        root.Calculator = {
                add: function(a, b) { return a + b; },
                sub: function(a, b) { return a - b; }
        };

}(typeof module == 'undefined' ? this : module.exports));

When I run node, I can import the module: 运行节点时,可以导入模块:

> var c = require('./module.js')
undefined
> c
{ Calculator: { add: [Function], sub: [Function] } }

but what can I do to import submodule? 但是我该怎么导入子模块?

socket.io is good example maybe. socket.io就是一个很好的例子。 You can see structure from there. 您可以从那里看到结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM