CommonJS,AMD和RequireJS之间的关系? [英] Relation between CommonJS, AMD and RequireJS?

查看:148
本文介绍了CommonJS,AMD和RequireJS之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然对CommonJS,AMD和RequireJS感到困惑。即使阅读了很多。

I'm still very confused about CommonJS, AMD and RequireJS. Even after reading a lot.

我知道CommonJS(以前称为ServerJS)是一个用于在浏览器外部使用该语言时定义一些JavaScript规范(即模块)的组。 CommonJS模块规范有一些实现,比如Node.js或RingoJS,对吗?

I know that CommonJS (formerly ServerJS) is a group for defining some JavaScript specifications (i.e. modules) when the language is used outside the browser. CommonJS modules specification has some implementation like Node.js or RingoJS, right?

CommonJS,异步模块定义(AMD)和RequireJS之间的关系是什么? RequireJS是CommonJS模块定义的实现吗?如果是,那么AMD是什么?

What's the relation between CommonJS, Asynchronous Module Definition (AMD) and RequireJS? Is RequireJS an implementation of CommonJS module definition? If yes, what's AMD then?

推荐答案

RequireJS 实施 AMD API (来源)

CommonJS 是一种在 exports 对象的帮助下定义模块的方法,它定义了模块内容。简单地说,CommonJS实现可能如下所示:

CommonJS is a way of defining modules with the help of an exports object, that defines the module contents. Simply put, a CommonJS implementation might work like this:

// someModule.js
exports.doSomething = function() { return "foo"; };

//otherModule.js
var someModule = require('someModule'); // in the vein of node    
exports.doSomethingElse = function() { return someModule.doSomething() + "bar"; };

基本上,CommonJS指定您需要 require()用于获取依赖关系的函数, exports 变量用于导出模块内容和模块标识符(用于描述与此模块相关的模块的位置)用于要求依赖项( source )。 CommonJS有各种各样的实现,包括你提到的 Node.js

Basically, CommonJS specifies that you need to have a require() function to fetch dependencies, an exports variable to export module contents and a module identifier (which describes the location of the module in question in relation to this module) that is used to require the dependencies (source). CommonJS has various implementations, including Node.js, which you mentioned.

CommonJS并没有特别考虑到浏览器,所以它没有'非常适合在浏览器环境中使用(我真的没有这方面的来源 - 它在任何地方都这么说,包括 RequireJS网站。 )显然,这与异步加载等有关。

CommonJS was not particularly designed with browsers in mind, so it doesn't fit in the browser environment very well (I really have no source for this--it just says so everywhere, including the RequireJS site.) Apparently, this has something to do with asynchronous loading, etc.

另一方面,RequireJS实现AMD,旨在适应浏览器环境(来源)。显然,AMD最初是作为CommonJS Transport格式的衍生产品,并发展成为自己的模块定义API。因此两者之间的相似之处。 AMD的新功能是 define()函数,它允许模块在加载之前声明其依赖项。例如,定义可以是:

On the other hand, RequireJS implements AMD, which is designed to suit the browser environment (source). Apparently, AMD started as a spinoff of the CommonJS Transport format and evolved into its own module definition API. Hence the similarities between the two. The new feature in AMD is the define() function that allows the module to declare its dependencies before being loaded. For example, the definition could be:

define('module/id/string', ['module', 'dependency', 'array'], 
function(module, factory function) {
  return ModuleContents;  
});

因此,CommonJS和AMD是具有不同实现的 JavaScript 模块定义API ,但两者都来自同一个来源。

So, CommonJS and AMD are JavaScript module definition APIs that have different implementations, but both come from the same origins.


  • AMD 更适合浏览器,因为它支持异步加载模块依赖项。

  • RequireJS AMD 的实现,同时试图保持 CommonJS (主要在模块标识符中)。

  • AMD is more suited for the browser, because it supports asynchronous loading of module dependencies.
  • RequireJS is an implementation of AMD, while at the same time trying to keep the spirit of CommonJS (mainly in the module identifiers).

为了让你更加困惑,需要使用RequireJS,同时作为AMD实现,提供CommonJS包装器,因此几乎可以直接导入CommonJS模块以与RequireJS一起使用。

To confuse you even more, RequireJS, while being an AMD implementation, offers a CommonJS wrapper so CommonJS modules can almost directly be imported for use with RequireJS.

define(function(require, exports, module) {
  var someModule = require('someModule'); // in the vein of node    
  exports.doSomethingElse = function() { return someModule.doSomething() + "bar"; };
});

我希望这有助于澄清事情!

I hope this helps to clarify things!

这篇关于CommonJS,AMD和RequireJS之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆