Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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 CommonJS模块在哪里?_Javascript_Node.js_Commonjs - Fatal编程技术网

Javascript CommonJS模块在哪里?

Javascript CommonJS模块在哪里?,javascript,node.js,commonjs,Javascript,Node.js,Commonjs,我时常听说CommonJS是一种创建一组模块化javascript组件的努力,但坦率地说,我对它一无所知 我可以使用的这些模块化组件在哪里?我在他们的主页上看不到太多内容。CommonJS只是一个标准,指定了一种模块化JavaScript的方法,因此CommonJS本身不提供任何JavaScript库 CommonJS指定了一个require()函数,该函数允许导入模块并使用它们,模块有一个名为exports的特殊全局变量,该变量是一个保存将导出的内容的对象 // foo.js --------

我时常听说CommonJS是一种创建一组模块化javascript组件的努力,但坦率地说,我对它一无所知


我可以使用的这些模块化组件在哪里?我在他们的主页上看不到太多内容。

CommonJS只是一个标准,指定了一种模块化JavaScript的方法,因此CommonJS本身不提供任何JavaScript库

CommonJS指定了一个
require()
函数,该函数允许导入模块并使用它们,模块有一个名为
exports
的特殊全局变量,该变量是一个保存将导出的内容的对象

// foo.js ---------------- Example Foo module
function Foo() {
    this.bla = function() {
        console.log('Hello World');
    }
}

exports.foo = Foo;

// myawesomeprogram.js ----------------------
var foo = require('./foo'); // './' will require the module relative
                            // in this case foo.js is in the same directory as this .js file
var test = new foo.Foo();
test.bla(); // logs 'Hello World'
Node.js标准库和所有第三方库使用CommonJS模块化其代码

还有一个例子:

// require the http module from the standard library
var http = require('http'); // no './' will look up the require paths to find the module
var express = require('express'); // require the express.js framework (needs to be installed)

这个想法,似乎(我没有意识到这一点),是为不仅仅是web浏览器提供javascript。例如,支持javascript进行查询。

CommonJS不是一个模块,它只是一个规范,定义了两个javascript模块应该如何相互通信。本规范使用exports变量和require函数来定义模块如何相互公开和使用

为了实现CommonJS规范,我们有很多遵循CommonJS规范的开源JS框架。JS加载程序的一些例子有systemJS、Webpack、RequireJS等等。下面是一个简单的视频,它解释了CommonJS,还演示了systemJS如何实现CommonJS规范


Common JS视频:-

那么CommonJS只指定require()?就这样?当你读到它时,它听起来“更大”:@wend它还定义了一个匿名包装函数来封装模块内容,但该函数的具体外观还是留给实现的,
require
函数和
exports
对象是实现必须共享的。是的,他们网站上的所有规范和标准听起来都很重要:DSo commonJS只是大多数库采用的一种导出方式?它不是源于一段派生的代码,但是可以随时与JS一起使用,而无需任何安装?