为什么阵列仍具有删除之后非零长度? [英] Why does the array still have a non-zero length after deletion?

查看:80
本文介绍了为什么阵列仍具有删除之后非零长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code,输出阵列的长度,将其删除,然后输出的新长度:

 的console.log($ scope.adviceList.activeAdvices.length); // * 1 *
$ scope.adviceList.activeAdvices.splice(ID,1); // * id是一个参数*
的console.log($ scope.adviceList.activeAdvices.length); // * 0 *的console.log($ scope.adviceList.activeAdvices.length); // * 1 *
删除$ scope.adviceList.activeAdvices [ID]
的console.log($ scope.adviceList.activeAdvices.length); // * 0 *
的console.log($ scope.adviceList.activeAdvices); // * [] *

删除后,该数组正确显示为空。它的长度,但是,仍然是1。


解决方案

删除是一个低级别操作符。它直接作用在物体上,它并不知道的阵列。使用删除根本不触发程序重新计算数组长度。


其他答案在这里宣称该属性的值设置为 undfined ,这是不正确。一个简单的测试表明:

  VAR A = [1];
删除一个[0];
console.dir(一);

和比较,如果你真的值设置为会发生什么未定义

  VAR A = [1];
一个[0] =未定义;
console.dir(一);

有关更确凿的证据,让我们来看看在规范


  

当[删除]中的 0 的调用与属性名的 P 的和布尔标志的的,下面的步骤内部方法采取:


  
  

      
  1. 我们的说明的是调用的 0 的的[[GetOwnProperty]]内部方法,属性名的 P 的结果。

  2.   
  3. 如果说明的是不确定的,则返回真正

  4.   
  5. 如果说明的[[配置]是的真正,然后结果
        一个。与名称中删除自己的财产的 P 的距离的 0 的。结果
        湾返回的真正

  6.   
  7. 否则,如果扔,则抛出一个类型错误例外。

  8.   
  9. 返回

  10.   

无处据说属性的值设置为未定义


不同浏览器的控制台可能显示阵列的不同重新presentations。在这个特殊的例子,你可以争论是否应该 [] [未定义]


  • [] (铬)似乎是有道理的,因为数组真的没有任何元素,有与数字域名没有属性。但是,当您访问。长度属性,你会得到一个 1 ,这可能会造成混淆。结果
    不久以前,Chrome的使用重新presentation像 [未定义×5] 指示长度为5,但没有元素的数组。我认为这实际上是一个很好的解决方案。


  • [未定义] (火狐)是有道理的,因为数组的长度是1和访问的改编[0] 实际上返回未定义(但这样做改编[10] ),但 arr.hasOwnProperty(0)如果数组包含真值未定义,怎么能只由这再presentation从一个长度的空数组区分(答案:你不能)。


的底线是:不要相信的console.log 太多。如果你想要一个确切的再presentation而使用 console.dir

I have the following code that outputs the length of an array, deletes it, and then outputs the new length:

console.log($scope.adviceList.activeAdvices.length); // *1*
$scope.adviceList.activeAdvices.splice(id,1); // *id is a parameter*
console.log($scope.adviceList.activeAdvices.length); // *0*

console.log($scope.adviceList.activeAdvices.length); // *1*
delete $scope.adviceList.activeAdvices[id];
console.log($scope.adviceList.activeAdvices.length); // *0*
console.log($scope.adviceList.activeAdvices); // *[]*

After the deletion, the array is correctly displayed as empty. Its length, however, is still 1.

解决方案

delete is a "lower level" operator. It directly works on the object, it doesn't "know" about the array. Using delete simply doesn't trigger the routine to recompute the array length.


Other answers here claim that the value of the property is set to undfined, which is incorrect. A simple test shows that:

var a = [1]; 
delete a[0]; 
console.dir(a);

and compare to what happens if you really set the value to undefined:

var a = [1]; 
a[0] = undefined; 
console.dir(a);

For a more solid proof, lets have a look at the specification:

When the [[Delete]] internal method of O is called with property name P and the Boolean flag Throw, the following steps are taken:

  1. Let desc be the result of calling the [[GetOwnProperty]] internal method of O with property name P.
  2. If desc is undefined, then return true.
  3. If desc.[[Configurable]] is true, then
       a. Remove the own property with name P from O.
       b. Return true.
  4. Else if Throw, then throw a TypeError exception.
  5. Return false.

Nowhere it is said that the value of the property is set to undefined.


The consoles of different browsers might show different representations of the array. In this particular example, you can argue whether it should be [] or [undefined].

  • [] (Chrome) seems to make sense because the array really doesn't have any elements, there are no properties with numeric names. However, when you access the .length property, you would get a 1, which can be confusing.
    Not too long ago, Chrome used a representation like [undefined x 5] to indicate an array of length 5 but without elements. I think this was actually a good solution.

  • [undefined] (Firefox) makes sense because the array is of length 1 and accessing arr[0] actually returns undefined (but so does arr[10]).However, arr.hasOwnProperty(0) would be false and if an array really contains the value undefined, how can it be distinguished from an empty array of length one solely by this representation (answer: you can't).

The bottom line is: Don't trust console.log too much. Rather use console.dir if you want an exact representation.

这篇关于为什么阵列仍具有删除之后非零长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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