ES6变量通过引用或复制导入 [英] ES6 variable import by reference or copy

查看:100
本文介绍了ES6变量通过引用或复制导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 var.js

export let x = 1;
export const f = () => x = 5;

然后我在另一个文件中执行此操作

Then i execute this in another file

import { x, f } from './var.js';
console.log(x); // 1
f();
console.log(x); // 5

为什么导入的变量 x 能够相应改变吗?

Why is the imported variable x able to change accordingly?

x import {x} c>在 var.js 更改?

Does import { x } gets re-evaluated when x in var.js changes?

或者 x var.js 中对原始 x 的引用而不是副本?

Or is x a reference to the original x in var.js rather than a copy?

推荐答案

ES6导入/导出实际上是绑定(引用)。当原始文件 var.js 中的 x 的值发生变化时,它也会反映在另一个文件中。

ES6 import/exports are actually bindings (references). As the value of x in original file var.js changes, it's reflected in another file too.

参考: http:// 2ality .com / 2015/07 / es6-module-exports.html

这篇关于ES6变量通过引用或复制导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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