You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于delete操作全局对象属性的误区及相关技术疑问

Great question! Let's break this down step by step to clear up your confusion.

数组元素是不是数组对象的属性?

Absolutely yes. Arrays in JavaScript are just specialized objects under the hood. The indices you use to access array elements (like 0, 1, 2) are actually string-based property names for the array object. For example, testing[0] is exactly equivalent to testing["0"] — both refer to the same property on the array object. The only special thing about these index properties is that they're linked to the array's length attribute (more on that later).

用delete操作数组元素的结果

Let's take your example: var testing = ["test1", "test2", "test3"]. If you run delete testing[0], this operation will succeed — but with a catch.

What happens is that the 0 property is removed from the array object, leaving an "empty slot" in its place. The array's length won't change, so you'll end up with [empty, "test2", "test3"] (you might see undefined in some environments, but it's technically an uninitialized slot).

This is different from methods like splice(), which actually removes the element and shifts the remaining elements to fill the gap, adjusting the length accordingly. delete only removes the property (key-value pair) from the array object, it doesn't reindex the array.

能不能delete数组的length属性?

No, you can't. The length property of an array is a built-in attribute with a non-configurable (configurable: false) property descriptor. This means the property can't be deleted, regardless of how the array was declared (even if it's a global array created with var).

If you try delete testing.length, the operation will return false (indicating failure), and the length value will remain unchanged.

关于文档中“无法删除var声明的全局对象属性”的说明

Let's clarify this point because it's easy to mix up. When you declare a global variable with var (like var testing = [...]), that variable becomes a property of the global object (window in browsers, global in Node.js). This property has a non-configurable descriptor, so delete window.testing will fail — you can't remove the global variable itself.

But this rule only applies to the global variable property (window.testing), not to the properties of the object that variable points to (like testing[0] or testing.length). The array's own properties (elements, length) follow their own rules, as we explained earlier.

Example code to demonstrate all this:

var testing = ["test1", "test2", "test3"];

// Delete array element
delete testing[0];
console.log(testing);       // Output: [empty, "test2", "test3"]
console.log(testing.length);// Output: 3

// Try deleting length
delete testing.length;
console.log(testing.length);// Output: 3 (delete failed)

// Try deleting the global variable itself
delete window.testing;
console.log(window.testing);// Still exists (delete failed)

内容的提问来源于stack exchange,提问作者Zakaria Sichaib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:55:38