CommonJS是一个可以搭载的标准,还是仅仅是另一个JavaScript模块系统? [英] Is CommonJS a standard that can be piggybacked on or is it just another JavaScript module system?

查看:68
本文介绍了CommonJS是一个可以搭载的标准,还是仅仅是另一个JavaScript模块系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wikipedia声明有关CommonJS:

Wikipedia states this about CommonJS:

[CommonJS]是一个旨在为以下项目指定生态系统的项目: 浏览器之外的JavaScript

[CommonJS] is a project with the goal of specifying an ecosystem for JavaScript outside the browser

我知道这意味着CommonJS是一个与使用ES的平台无关的系统.这是否意味着当在Web浏览器的上下文中使用时,对于ES模块系统而言,它实质上只是另一个大型的polyfill(例如SystemJS)?还是位于诸如SystemJS或WebPack之类的模块系统下的东西,使该系统平台不可知?

I understand that this means that CommonJS is a system that's intended to be agnostic as to the platform that ES is being used on. Does that mean that when used in the context of a web browser that it's essentially just another big polyfill (like SystemJS), for the ES module system? Or is it something that sits underneath a module system like SystemJS or WebPack that renders that system platform agnostic?

推荐答案

与SystemJS不同,CommonJS本身是

Unlike SystemJS, CommonJS itself is a specification, not a module loader. It is platform agnostic in that it relies on core ECMAScript 5 syntax/features; it specifies a set of fields to define - module.exports - and method to read them - require - to facilitate better code organization. It might be thought of as an unofficial precursor to ES6 module syntax. AMD (Asynchronous Module Definition) is a competing unofficial standard, forked from CommonJS and implemented by Require.js.

就像非官方的一样,浏览器不包含对CommonJS模块的任何本机支持,并且使用需要某种模块加载器来实现对CommonJS的支持(例如Browserify). Node.js实现了CommonJS规范,但是即使将其模块执行包装在运行时的功能.

Likely as it is unofficial, browsers don't include any native support for CommonJS modules, and usage requires some kind of module loader implementing support for CommonJS (like Browserify). Node.js implements the CommonJS specification, but even it wraps its module executions in a function at run-time.

CommonJS属于较早的模式( 是基层工作",而不是正式的ECMAScript规范添加),旨在解决JavaScript缺乏代码组织的问题.根据项目的网站),它是在2009年创建的,大约与Node.js <一个href ="https://github.com/nodejs/node-v0.x-archive/tags?after=v0.0.4" rel ="nofollow noreferrer">最初发布,旨在提供模块结构适用于非浏览器执行环境(尽管它可以轻松地应用于大型脚本和全局变量同样存在问题的浏览器).该项目是根据此博客文章<创建的. /a>.

CommonJS is among the earlier patterns (described as "a grass roots effort", as opposed to an official ECMAScript spec addition) intended to account for JavaScript's lack of code organization. According to the project's website) it was created in 2009, around the same time Node.js was originally released, and intended to provided module structure for non-browser execution environments (although it could easily apply to browsers where mega-scripts and globals are just as problematic). The project was founded as result of this blog post.

因此,要回答您的标题问题,这是一个非官方的标准,当(几乎)普遍支持ES6模块时,该标准将会过时.

So to answer your title question, it is an unofficial standard that will be obsolete when ES6 modules are (nearly) universally supported.

这篇关于CommonJS是一个可以搭载的标准,还是仅仅是另一个JavaScript模块系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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