Home Web Front-end JS Tutorial JavaScript delete operator application examples_javascript skills

JavaScript delete operator application examples_javascript skills

May 16, 2016 pm 06:56 PM
delete javascript Operator

Today when I was looking at the prototype code, I discovered the delete operator

Copy the code The code is as follows:

unset: function(key) {
var value = this._object[key];
delete this._object[key];
return value;
}

I checked the manual and found that the
delete operator
deletes an attribute from an object or deletes an element from an array.
delete expression
expression parameter is a valid JScript expression, usually a property name or array element.
Explanation
If the result of expression is an object and the attribute specified in expression exists, and the object does not allow it to be deleted, return false.
In all other cases, returns true.
It feels good to see "Delete an element from an array", but after trying it in ff, it seems that it can only delete the value of that element, not the element itself. However, it is possible to delete a property from an object.
I googled again and found an article that was very detailed. I reprinted it here to avoid forgetting:
Javascript variables
In fact, in Javascript, variables = object properties. This is because Javascript will know before executing the script. Create a Global object. All global variables are properties of this Global object. When executing a function, an Activation object is also created. All local variables are properties of this Activation object. For example:
Copy code The code is as follows:

var global = 42;
this .global; // 42, the Global object can be accessed through this
this.global2 = 12;
global2; // 12
function foo() {
var local = 36;
// However, Activation cannot be accessed directly,
// Therefore, local variables cannot be accessed through foo.local
}

Objects deleted by delete operator
are also in C The delete operator deletes the object pointed to by the pointer. For example:
Copy code The code is as follows:

// C
class Object {
public:
Object *x;
}
Object o;
o.x = new Object();
delete o.x; // The Object object new in the previous line will be released

But Javascript's delete is different from C. It will not delete the object pointed to by o.x, but delete the o.x attribute itself.
Copy code The code is as follows:

// Javascript
var o = {};
o.x = new Object();
delete o.x; // The new Object in the previous line still exists
o.x; // undefined, the attribute named x of o is deleted

In actual Javascript, after deleting o.x, the Object object will be garbage collected due to loss of reference, so deleting o.x is "equivalent" to deleting the object pointed to by o.x, but this action is not an ECMAScript standard , that is to say, even if an implementation does not delete the Object object at all, it does not violate the ECMAScript standard.
"Deleting attributes instead of deleting objects" can be confirmed by the following code.
Copy code The code is as follows:

var o = {};
var a = { x: 10 };
o.a = a;
delete o.a; // The o.a attribute is deleted
o.a; // undefined
a.x; // 10, because the { x: 10 } object remains It is referenced by a, so it will not be recycled

In addition, delete o.x can also be written as delete o["x"], both of which have the same effect.
The situation of executing delete on a variable
Since the variable is also a property of the Global or Activation object, the delete operation on the variable also has the same result.
Copy code The code is as follows:

var global = 42;
delete global; / / Delete Global.global
function foo() {
var local = 36;
delete local; // Delete Activation.local
}

attributes that can be deleted And attributes that cannot be deleted
Not all attributes can be deleted. For example, attributes declared in prototype cannot be deleted:
Copy code The code is as follows:

function C() { this.x = 42; }
C.prototype.x = 12;
var o = new C();
o.x; // 42, construction o.x defined in the function
delete o.x;
o.x; // 12, o.x defined in the prototype will not be deleted even if delete o.x is executed again

Predefined properties of the object It cannot be deleted either. This type of attribute can be considered to have the characteristics of DontDelete.
Copy code The code is as follows:

var re = /abc/i;
delete re.ignoreCase;
re.ignoreCase; // true, ignoreCase cannot delete

Variables that can be deleted and variables that cannot be deleted
Variables declared through var and functions declared through function It has the DontDelete attribute and cannot be deleted.
Copy code The code is as follows:

var x = 36;
delete x;
x; // 36, x is not deleted
y = 12;
delete y;
y; // undefined
function foo() { return 42; }
delete foo ;
foo(); // 42

But there is one exception, that is, in the code executed through eval, although the variables declared through var belong to the same Global object as the normal var declared variables, But they do not have the DontDelete attribute and can be deleted.
Copy code The code is as follows:

eval("var x = 36;");
x; // 42
delete x;
x; // undefined

But there is one exception. Variables defined by var within the function in the eval code have DontDelete , cannot be deleted.
Copy code The code is as follows:

eval("(function() { var x = 42 ; delete The rule is: when the property of the deleted object exists and has DontDelete, it returns false, otherwise it returns true. One feature here is that true is returned even when the object attribute does not exist, so the return value is not completely equivalent to whether the deletion is successful or not.



Copy code
The code is as follows:function C() { this.x = 42; } C.prototype.y = 12; var o = new C();
delete o.x; // true
o.x; // undefined
"x" in o; // false
// o.x exists and has no DontDelete, return true
delete o.y; // true
o.y; // 12
// o itself has no o.y attribute, so return true
// From here you can also see the existence of the prototype chain. The object's own properties and the prototype properties are different
delete o; // false
// Global.o has the DontDelete feature so it returns false
delete undefinedProperty; / / true
// Global has no property named undefinedProperty so returns true
delete 42; // true
// 42 is not a property so returns true. Some implementations will throw exceptions (violating ECMAScript standards)
var x = 24;
delete x; // true
x; // 25
// What is deleted is the return value of x (24), is not an attribute, so it returns true

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles