实时绑定是什么意思? [英] What does it mean by live bindings?

查看:86
本文介绍了实时绑定是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一个教程,上面写着

I am following a tutorial and it says

ES模块使用实时绑定.这意味着支持周期性的功能依赖性.

ES modules uses live bindings. It means a feature to support cyclical dependencies.

但是我不清楚这个概念.这是什么意思?

But I don't clearly understand this concept. What does this mean?

推荐答案

实时绑定是ES模块中引入的一个概念.这意味着,当导出模块更改值时,更改将在导入方可见.CommonJS模块不是这种情况.模块导出被复制到CommonJS中.因此,导入模块无法看到导出方发生的更改.

Live bindings is a concept introduced in ES modules. It means that when the exporting module changes a value, the change will be visible from the importer side. This is not the case for CommonJS modules. Module exports are copied in CommonJS. Hence importing modules cannot see changes happened on the exporter side.

export let count = 1;
export function increment() {
    ++count;
}

index.mjs

import { count, increment } from './counter.mjs';
console.log(count);
increment();
console.log(count);

输出

$ node --experimental-modules index.mjs
1
2


CJS

counter.js

let count = 1;
function increment() {
    ++count;
}

exports.count = count;
exports.increment = increment;

index.js

const { count, increment } = require('./counter.js');
console.log(count);
increment();
console.log(count);

输出

$ node index.js
1
1


有关该主题的更多资源:


More resources on the topic:

  • What do ES6 modules export? by Axel Rauschmayer
  • ES modules: A cartoon deep-dive by Lin Clark
  • Chapter on Modules in Exploring JS by Axel Rauschmayer

这篇关于实时绑定是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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