Javascript 参考与绑定...有什么区别? [英] Javascript reference vs binding...what's the difference?

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

问题描述

我最近在 Kyle Simpson 的你不知道的 JS:ES6 中阅读了以下内容

I recently read the following in Kyle Simpson's You Don't Know JS: ES6

[ES6 模块导出] 实际绑定(几乎类似于指针)到内部模块定义中的标识符."

"[ES6 modules export] actual bindings (almost like pointers) to the identifiers in your inner module definition."

我的困惑是这些绑定与引用有何不同...

My confusion is how these bindings differ from references...

我知道JS中的引用仅适用于非原始类型(如对象),因此给定

I understand that a reference in JS is only applicable to non-primitive types (like objects), so that given

let object1 = {a: 1};
let object2 = object1;

object1object2 现在引用(它们都是引用)同一个对象.
如果我向 object2 添加一个属性,我也会向 object1

object1 and object2 now refer to (they are both references to) the same object.
If I add a property to object2, I am also adding a property to object1

object2.b = 2;
console.log(object1.b); // 2

我可以看到绑定可以同时应用于原始类型和非原始类型

And I can see that a binding can apply to both primitive types and non-primitive types

// foo.js
export let count = 1;
export function incrementCount() { count++; }

// bar.js
import {count, incrementCount} from foo;
console.log(count); // 1
incrementCount();
console.log(count); // 2

绑定是否就像引用一样,除了原始值也可以共享绑定(而引用仅限于非原始类型)?

Is a binding just like a reference, except that primitive values can also share a binding (while references are limited to non-primitive types)?

我觉得我在这里遗漏了一些东西......

I feel like I'm missing something here...

推荐答案

A binding 是一个非常通用的术语,表示名称所指的内容".作用域中的每个标识符都绑定到某些东西.通常,它们解析为变量环境中的变量(环境记录),但也有例外(例如with 或全局对象).

A binding is a very generic term for "what a name refers to". Every identifier in a scope is bound to something. Usually they resolve to variables in a variable environment (storage slots in an environment record), but there are exceptions (e.g. with or the global object).

reference 是指针的术语到某种结构.例如,对象被称为引用值",因为它们引用具有标识的可变属性的容器.

A reference is a term for a pointer to some kind of structure. For example, objects are known as "reference values" because they reference the container of mutable properties with an identity.

ES6 模块现在引入了一种新的绑定类型,这是以前未知的.它不是通常的变量,而是字面上对另一个变量的引用 - 从另一个模块导出的变量.如果模块变量发生变化,这将通过导入反映出来——它们都指向同一个环境记录槽.
export 声明添加了从本地名称到 模块接口,而 import 声明添加了从相应模块接口中的名称到本地名称的映射.当一个模块被实例化时,一个间接绑定 指向与导出模块中的本地绑定相同的环境.

ES6 modules are now introducing a new type of binding, one that was unknown before. It is not a usual variable, but literally a reference to another variable - the one exported from the other module. If the module variable changes, this will be reflected by the import - they both point to the same environment record slot.
An export declaration adds a mapping from a local name to a name in the module interface, while an import declaration adds a mapping from a name in the respective module interface to a local name. When a module is instantiated, an indirect binding is created that points to the same environment as the local binding in the exporting module.

这篇关于Javascript 参考与绑定...有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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