简体   繁体   English

什么是 ARRAY:SORT 的 ECMA 规范中的“不存在的属性值”

[英]What are "non-existent property values" in ECMA spec for ARRAY:SORT

In an answer for this question (which otherwise I can fully understand/etc), there'a this curious quite:这个问题的答案中(否则我可以完全理解/等),有一个很好奇的:

From the spec, 15.4.4.11 :根据规范, 15.4.4.11 :

Because non-existent property values always compare greater than undefined property values, and undefined always compares greater than any other value, undefined property values always sort to the end of the result, followed by non-existent property values .因为不存在的属性值总是比较大于未定义的属性值,并且未定义的总是比较大于任何其他值,所以未定义的属性值总是排序到结果的末尾,然后是不存在的属性值

I've checked in the latest version available now and it's "note 1" at the end of sort spec, and it's basically the same as it was when that answer from 2011 was written.我已经检查了现在可用的最新版本,它是sort规范末尾的“注释 1”,它与编写 2011 年的答案时基本相同。

Regarding undefined property values always sort to the end of the result, followed by non-existent property values -- how can it be?关于undefined property values always sort to the end of the result, followed by non-existent property values ——怎么可能呢? what are "non-existent property values"(*)?什么是“不存在的属性值”(*)? if we write a.foo and a doesn't have such property, we'll get undefined , so how can it be differentiated?如果我们写a.foo并且a没有这样的属性,我们会得到undefined ,那么如何区分呢?

The sort is called either without any parameter, or with a comparer-style function, and in the latter case, it's our function, and we're bound to read the non-existent property and get undefined .. The sort can't inspect the object's keys for us to decide whever an inspected object has a property or not (in contrast to ie certain underscore/lodash helpers where you define a 'path' like ie pluck or get ). sort被调用时不带任何参数,或者使用比较器风格的函数,在后一种情况下,它是我们的函数,我们必须读取不存在的属性并得到undefined .. sort无法检查对象的键让我们决定被检查的对象是否具有属性(与某些下划线/lodash 帮助器相反,您在其中定义“路径”,例如pluckget )。 I just dont see how we could trigger this "non-existent property values" case at all.我只是根本看不到我们如何触发这种“不存在的属性值”案例。

(*) I've found something that looks like a definition of this term here : (*) 我在这里找到了类似于该术语定义的内容:

A non-existent property is a property that does not exist as an own property on a non-extensible target.不存在的属性是在不可扩展目标上不作为自有属性存在的属性。 (...) If the target is non-extensible and P is non-existent, then all future calls to [[GetOwnProperty]] (P) on the target must describe P as non-existent (ie [[GetOwnProperty]] (P) must return undefined). (...) 如果目标是不可扩展的并且 P 不存在,那么未来对目标的 [[GetOwnProperty]] (P) 的所有调用都必须将 P 描述为不存在(即 [[GetOwnProperty]] ( P) 必须返回 undefined)。

This must-describe-as-nonexistent and must-return-undefined seem to support my doubt.这个必须描述为不存在和必须返回未定义似乎支持我的怀疑。

I've also noticed that the pseudo-code for SortIndexedProperties (used to define sort ) actually contains bits like 3.b. Let kPresent be ? HasProperty(obj, Pk).我还注意到SortIndexedProperties的伪代码(用于定义sort )实际上包含像3.b. Let kPresent be ? HasProperty(obj, Pk).这样的位。 3.b. Let kPresent be ? HasProperty(obj, Pk). . . So maybe that non-existent property part in sort spec meant to cover some case like the array being mutated by the comparer function and certain keys are removed from it?因此, sort规范中non-existent property部分可能是为了涵盖某些情况,例如数组被比较器函数变异并且某些键被从中删除?

A non-existent property on an array will be "included" when sorting only if the array is sparse.仅当数组稀疏时,才会在排序时“包含”数组上不存在的属性。 Here's an example, look at the comments:举个例子,看评论:

 const arr = []; // 0-length array arr[2] = 'foo'; // Turns into 3-length array. Does not have own properties 0 or 1 arr.sort(); console.log(arr[0]); // Sort result includes a value from defined property console.log(arr.hasOwnProperty(1)); // But not from the sparse elements console.log(arr.length); // But the array will have the same length as originally

As you can see, the properties that didn't exist on the original sparse array are "sorted" to the end of the array, and don't exist on the resulting sorted array either, except for the .length of the sorted array.如您所见,原始稀疏数组上不存在的属性被“排序”到数组的末尾,并且也不存在于生成的排序数组中,除了排序数组的.length It started out as an array with a length of 3 and only one own-property, and ended up as an array with a length of 3 and only one own-property (but with that own-property at a different index).它开始是一个长度为 3 且只有一个自有属性的数组,最终是一个长度为 3 且只有一个自有属性的数组(但该自有属性位于不同的索引处)。

if we write a.foo and a doesn't have such property, we'll get undefined, so how can it be differentiated?如果我们写 a.foo 并且 a 没有这样的属性,我们会得到 undefined,那么如何区分呢?

It's equivalent to a .hasOwnProperty check:它相当于一个.hasOwnProperty检查:

 const obj1 = {}; const obj2 = { foo: undefined }; console.log(obj1.hasOwnProperty('foo')); console.log(obj2.hasOwnProperty('foo')); const arr1 = ['abc']; const arr2 = [, 'def']; // sparse array console.log(arr1.hasOwnProperty(0)); console.log(arr2.hasOwnProperty(0));

Best approach to all of this - never use sparse arrays to begin with, they're too confusing.所有这一切的最佳方法 - 从不使用稀疏数组开始,它们太混乱了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM