如何解释ECMAScript术语中的对象引用? [英] How to explain object references in ECMAScript terms?

查看:111
本文介绍了如何解释ECMAScript术语中的对象引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下:

var a = {}, b = a;

就规格而言, b = a 归结为 PutValue(b,GetValue(a)),对吧?并且 GetValue(a)使用 GetBindingValue(a,strictFlag)抽象操作,返回值 A 。而值是最初分配给 a 的对象。然后对象存储在 b 中,就像任何其他值一样。

In terms of the spec, b = a boils down to PutValue(b, GetValue(a)), right? And GetValue(a) uses GetBindingValue("a", strictFlag) abstract operation, which returns "the value" in a. And "the value" is "the object" originally assigned to a. Then "the object" is stored in b, just like any other value would.

但是什么是对象确切地说?规范在哪里说Object类型的值与原语的行为不同?只是原语是不可变的,对象是可变的吗?

But what is "the object" precisely? Where does the specification say that values of the Object type behave differently than primitives? Is it only that primitives are immutable, and objects are mutable?

我在问,因为我们在试图解释时总是谈论对象引用和引用值对象的行为,但我找不到与规范中类似的任何东西。

I'm asking because we always talk about "object references" and "reference values" when trying to explain the behavior of objects, but I couldn't find anything analogous to that in the specification.

推荐答案


规范在哪里说Object类型的值与原语的行为不同?只是原语是不可变的,对象是可变的吗?

Where does the specification say that values of the Object type behave differently than primitives? Is it only that primitives are immutable, and objects are mutable?

是的,它基本上归结为对象的可变性,以及 身份对象。实际上,这甚至没有在任何地方指定,它只是假设为面向对象编程的给定核心思想。唯一提到的是 Annex E 中的评论,其中声明更改 [正则表达式文字表达式值] 的更改可被任何测试此类文字值的对象标识或对共享副作用敏感的程序检测到

Yes, it bascially boils down to the mutability of objects, and the identity of objects. Actually, this is not even specified anywhere, it is just assumed as a given core idea of object-oriented programming. The only mention of it is a comment in the Annex E, which states that "the change [of regex literal expression values] is detectable by any programs that test the object identity of such literal values or that are sensitive to the shared side effects".

即使对象的可变性在任何地方都没有明确说明,但短语暗示为在ECMAScript中,状态和方法由对象承载 ECMAScript对象是一组属性和几个更改属性值,创建属性或设置属性属性的概念(在 [[DefineOwnProperty]] 方法)。

Even the mutability of objects in nowhere explicitly stated, but implied by phrases as "In ECMAScript, the state and methods are carried by objects", "An ECMAScript object is a collection of properties" and severals notions of "changing" property values, "creating" properties or "setting" property attributes (in the [[DefineOwnProperty]] method).


我问,因为我们在尝试解释对象的行为时总是谈论对象引用和引用值,但我找不到任何东西类似于规范中的那些。

I'm asking because we always talk about "object references" and "reference values" when trying to explain the behavior of objects, but I couldn't find anything analogous to that in the specification.

这是因为规范不是该语言的指南,并且是对其功能的解释,但是仅仅是其(内部)特征的规范。读者应该知道OOP及其想法。

That's because the spec is not a guide to the language, and an explanation of its features, but merely a specification of its (inner) characteristics. The reader is expected to know OOP and its ideas.

事实上,语言总是只讨论值 - 无论是原始值还是对象。语言工具可以改变的唯一事情是环境记录(变量)和属性对象的绑定,其他一切(包括对象标识)被隐式地认为是不可变的。

In fact, the language always talks of values only - regardless whether that may be primitive values or objects. The only things that can be mutated by the tools of the language are the binding of Environment Records (variables) and the properties Objects, everything else (including object identiy) is implicitly considered immutable.

当我们试图解释对象的行为时,我们基本上解释了对象身份的概念。通常,受众来自较低级别的非OOP语言,默认情况下分配会复制,共享值由指针(引用)完成。对于他们,我们将对象解释为<参考属性集合,并且对象的所有外观都是指向同一集合的引用。没有内置的方法可以复制集合。

When we try to explain the "behavior of objects", we basically explain the concept of the identity of objects. Usually the audience is coming from lower-level, non-OOP languages, where assignments do copy by default, and sharing values is done by pointers (references). To them, we explain objects as a "reference to the collection of properties", and all appearances of an object are references that point to the same collection. There is no builtin way to copy the collection.

但是,为了强调一般 1 缺少引用(一个人无法引用一个标识符绑定,即一个变量 - 无论它的值是什么类型)并且为了符合官方的措辞,我们还使用术语来表示一切。这为对象创造了术语参考值。

However, to emphasize the lack of references in general1 (one cannot make a reference to an identifier binding, i.e. a variable - regardless of it's value's type) and to conform with the official phrasing, we also use the term value for everything. This coined the term "reference value" for objects.

此外, Sameness / 平等算法 (s)匹配: where x y ,...,[两者都是Object类型],...,返回 true 如果 x y 引用到同一个对象。

Also, the wording in the Sameness/Equality Algorithm(s) matches this: "where x and y are values, …, [when both are of type Object], …, return true if x and y refer to the same object."

1.实际上,规范描述了引用规格类型。它们确实表示对象的属性,并用于描述 delete ,属性赋值,方法调用等的行为。但它们无法传递(赋值,函数调用),仅限内部且无法获取且不会指向变量。仍然没有内置的方法来获取某种局部变量的指针。

1. Actually, the spec describes References as a Specification Type. They do denote properties of objects, and are used to describe the behaviour of delete, property assignments, method calls etc. They however cannot be passed around (assignment, function call), are internal only and not obtainable, and will not point to variables. Still, there is no builtin way to acquire some kind of pointer to a local variable.

这篇关于如何解释ECMAScript术语中的对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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