Javascript参考对绑定...有什么区别? [英] Javascript reference vs binding...what's the difference?
问题描述
[ES6模块导出]实际绑定(几乎像指针)到标识符在您的内部模块定义中。
我的困惑是这些绑定与引用不同...
了解JS中的引用仅适用于非原始类型(如对象),以便给定
let object1 = {a:1};
let object2 = object1;
object1
和 object2
现在引用(它们都是引用)同一个对象。
如果我添加一个属性到 object2
,我也添加了一个属性到 object1
object2.b = 2 ;
console.log(object1.b); // 2
我可以看到一个绑定可以应用于原始类型和非原始类型
// foo.js
export let count = 1;
export函数incrementCount(){count ++; }
// bar.js
import {count,incrementCount} from foo;
console.log(count); // 1
incrementCount();
console.log(count); // 2
是一个绑定,就像一个引用,除了原始值也可以共享一个绑定而引用仅限于非原始类型)?
我觉得我在这里缺少一些东西...
p> A binding 是一个非常通用的术语什么名称指。范围中的每个标识符都绑定到某个东西。通常它们可以解决变量环境中的变量(环境记录),但有例外(例如与
或全局对象)。
参考 是指向某个人的指针的术语一种结构。例如,对象被称为参考值,因为它们使用身份引用可变属性的容器。
ES6模块现在引入了一种新的绑定方式,以前是未知的绑定。它不是通常的变量,而是字面上引用另一个变量 - 从另一个模块导出的变量。如果模块变量发生变化,这将被导入反映 - 它们都指向相同的环境记录槽。
export
声明添加映射从本地名称到中的名称模块界面,而 import
声明会将相应模块界面中的名称映射添加到本地名称。当模块被实例化时, 间接绑定被创建,指向与导出模块中的本地绑定相同的环境。
I recently read the following in Kyle Simpson's You Don't Know JS: 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...
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;
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 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).
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 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屋!