> For the complete documentation index, see [llms.txt](https://xyy94813.gitbook.io/x-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xyy94813.gitbook.io/x-note/javascript/javascript-delete-yun-suan-fu.md).

# JavaScript delete 运算符

在 JavaScript 中，`delete` 是一元操作符，它用来删除对象属性或者数组元素。

```js
const obj = {
    a: 1,
    ['a-2']: 2, 
};
console.log('a' in obj); // true
console.log('a-2' in obj); // true


delete obj.a;
delete obj['a-2'];
console.log('a' in obj); // false
console.log('a-2' in obj); // false
```

> delete 只能删除自有属性，不能删除继承属性

```js
function Man(name) {
    this.name = name;
}
Man.prototype.sex = 1;

const alex = new Man('alex');
delete alex.name;
delete alex.sex;
console.log('name' in obj); // false
console.log('sex' in obj); // true
```

> `delete`实际上只是断开了属性和宿主对象的联系

```js
const obj = {
    a: {
        a1: 1,
    },
};
const p = obj.a;

delete obj.a;

console.log(p.a1); // 1

```

由于已经删除的属性的引用仍然存在，在某些 JavaScript 运行环境中，可能导致这部分的内存无法进行回收，最终造成内存泄露。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://xyy94813.gitbook.io/x-note/javascript/javascript-delete-yun-suan-fu.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
