Home > Web Front-end > JS Tutorial > body text

JavaScript delete operator application examples_javascript skills

WBOY
Release: 2016-05-16 18:56:39
Original
1384 people have browsed it

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

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