Home > Web Front-end > JS Tutorial > How to delete object attributes in javascript

How to delete object attributes in javascript

青灯夜游
Release: 2023-01-06 11:13:49
Original
8363 people have browsed it

In JavaScript, you can use the delete operator to delete the attributes of an object. Its operand should be an attribute access expression; for example "var obj={x:1};delete obj.x;" . The delete operator can only delete free attributes, not inherited attributes.

How to delete object attributes in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The delete operator can delete the properties of an object. Its operand should be a property access expression. What is surprising is that delete only disconnects the attribute from the host object, but does not operate the attributes in the attribute:

delete book.author; //book不再有属性author
delete book["main title"] //book不会再有属性"main title"
Copy after login

The delete operator can only delete free attributes, not inherited attributes (required Deleting an inherited property requires deleting it from the prototype object on which it is defined, and this affects all objects that inherit from this prototype).
Note:

a={p:{x:1}};
b=a.p;
delete a.p;
Copy after login

The value of b.x is still 1 after executing this code. Because references to deleted properties still exist, memory leaks may occur due to this loose code in some implementations of JavaScript. Therefore, when destroying the object, it is necessary to traverse the attributes in the attributes and delete them one by one.

When the delete expression is deleted successfully or without any side effects (such as deleting a non-existent attribute), it returns true. If delete is not a property access expression, delete also returns true.

o = {x:1};
delete o.x; //删除x,返回true。
delete o.x; //什么都没做(x已经不存在了),返回true
delete o.toString; //什么也没做(toString是继承来的),返回true
delete 1; //无意义,返回true
Copy after login

delete cannot delete properties whose configurability is false (although it can delete configurable properties of non-extensible objects). Some properties of built-in objects are not configurable, such as the properties of global objects created through variable declarations and function declarations. In strict mode, deleting a non-configurable property will raise a type error. In non-strict mode, the delete operator returns false in these cases:

delete Object.prototype; //不能删除,属性是不可配置的
var x = 1;
delete this.x; //不能删除这个全局变量,
function f(){}
delete this.f; //同样也不能删除全局函数
Copy after login

When deleting the global object's configurable and properties in non-strict mode, you can omit the global object For a reference, just follow the delete operator directly with the name of the attribute to be deleted:

this.x = 1; //创建一个可配置的全局属性(没有用var)
delete x; //将它删除
Copy after login

However, in strict mode, if delete is followed by an illegal operand (such as x), a report will be reported A syntax error, so the specified object and its properties must be displayed:

delete x; //在严格模式下报语法错误
delete this.x; //正常工作
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to delete object attributes in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template