[英]syntax differences between CJS & ES6 modules
In CJS modules I would use export
and var plugin = require('plugin');
在CJS模块中,我将使用export
和var plugin = require('plugin');
to export/import 出口/进口
In ES6 modules I would use export
and import * as plugin from 'plugin';
在ES6模块中,我将使用export
和import * as plugin from 'plugin';
to export/import. 导出/导入。
Are there more syntax differences? 还有更多语法差异吗? are these ^ differences correct? 这些^差异正确吗?
What does export default
and export *
? export default
和export *
什么?
CommonJS modules and ES6 modules are pretty similar, but they have some very important differences to be aware of. CommonJS模块和ES6模块非常相似,但是需要注意一些非常重要的区别。 To answer your question directly first: 首先直接回答您的问题:
var plugin = require('plugin');
in ES6 would be equivalent to both 在ES6中将等同于两者
// Import all named exports of 'plugin'.
import * as plugin from 'plugin';
and/or 和/或
// Import default export of 'plugin'.
import plugin from 'plugin';
// an alias of
import {default as plugin} from 'plugin';
but it depends on how 'plugin' has been written and whether it was written with ES6 export
or CommonJS module.exports
. 但是这取决于“插件”的编写方式以及它是使用ES6 export
还是CommonJS module.exports
。
CommonJS imports only have a single exported object. CommonJS导入只有一个导出对象。 That object may be a function, or an object, or anything. 该对象可以是函数,对象或任何东西。 Generally CommonJS modules do 通常CommonJS模块可以
exports.foo = ...;
exports.bar = ...;
to export named properties. 导出命名属性。 They may also export a 'default' object as 他们还可以将“默认”对象导出为
module.exports = function(){};
The core thing here is that if you want both a default export AND named exports, your only option is to actually put the properties directly onto the default export. 这里的核心是,如果要同时使用默认导出和命名导出,则唯一的选择是将属性直接直接放在默认导出上。
For ES6 modules, the concepts of named exports, and default exports are 100% separated. 对于ES6模块,命名导出和默认导出的概念是100%分开的。 eg 例如
export var foo = ...;
export var bar = ...;
export default function fn(){};
The main difference being that 主要区别在于
fn.foo !== foo;
With this example then, there are two cases 在这个例子中,有两种情况
export
用于ES6 export
插件 import * as plugin from 'plugin';
plugin.foo === ...;
plugin.bar === ...;
typeof plugin === 'object';
import plugin from 'plugin';
plugin.foo === undefined;
plugin.bar === undefined;
typeof plugin === 'function';
module.exports
插件使用CommonJS module.exports
import * as plugin from 'plugin';
plugin.foo === ...;
plugin.bar === ...;
typeof plugin === 'object';
import plugin from 'plugin';
plugin.foo === ...;
plugin.bar === ...;
typeof plugin === 'function';
The other primary difference in your example is that plugin
is a live binding. 您的示例的另一个主要区别是plugin
是实时绑定。 That means that if it is updated inside the module later, it will update itself in your import, eg 这意味着,如果稍后在模块内部对其进行更新,则它将在您的导入中进行更新,例如
// plugin.js
export var foo = 'foo';
export function update(){
foo = 'bar';
}
// other.js
import {foo, update} from 'plugin';
foo === 'foo';
update();
foo === 'bar'
and that would not be the case if you did 如果您这样做,情况就不会如此
var foo = require('plugin').foo;
var update = require('plugin').update;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.