Home >Web Front-end >JS Tutorial >Javascript study notes delete operator_javascript skills

Javascript study notes delete operator_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:02:251295browse
1. Syntax

The expression after delete must give a reference to an attribute, such as
var o = {a:1};
delete o.a; //this Where o.a is a reference to attribute a of object o

A separate attribute name can only be used in the with statement
Copy code The code is as follows:

with(o){
delete a;
}

2. Return value of delete

delete is a normal operator and will return true or false. The rule is: when the attribute of the deleted object exists and cannot be deleted, return false, otherwise return 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:

var o = {a:1};
delete o.a; //return true
var b = 2;
delete b;//return false, ECMA rule convention: variables declared using var and function cannot be deleted

3. Under which circumstances delete is not allowed

The variables declared by var and function mentioned in the above example cannot be deleted, but the implicit declaration can be deleted
Copy code The code is as follows:

function c(){return 12;}
delete c;//return false
d = function(){return 12;}
delete d;//return true

You cannot delete properties inherited from the prototype chain, but you can delete properties on the prototype chain
Copy code The code is as follows:

function Foo(){}
Foo.prototype .bar = 42;
var foo = new Foo();
delete foo.bar; // Returns true, but has no effect
alert(foo.bar); // alerts 42, attribute It is inherited
delete Foo.prototype.bar; // Delete the attribute bar on the prototype
alert(foo.bar); // alerts "undefined", the attribute no longer exists and cannot be inherited

4. Special cases
Copy code The code is as follows:
eval, they can be deleted
eval("var a=1");
delete a;
alert(a); //report Undefined error

A variable cannot be deleted if the declaration is made inside a closure in eval execution code
eval("(function(){var a=1;delete a ; return a;})()");//1

5. Delete array elements

Deleting its elements from the array will not affect the length of the array

Copy code The code is as follows:
var arr = ['yuyin','suhuan','baby' ];
delete arr[0];
alert(arr.length);//alert 3

The deleted key value no longer belongs to the array, but it can still be accessed , its value is undefined.

Copy code The code is as follows:
var arr = ['yuyin','suhuan', 'baby'];
delete arr[0];
0 in arr; // false
alert(arr[0]); //undefined
arr[0] === undefined; //true

Compare directly assigning the key value to undefined

Copy code The code is as follows:
var arr = ['yuyin','suhuan','baby'];
arr[0] = undefined;
0 in arr; // true
alert(arr [0]);//undefined
arr[0] === undefined;//true

It can be seen that the delete operation only deletes the key value attribute from the array. It is also an object in itself. This situation is easy to understand. If you need to retain the key value, you can use undefined assignment.
Statement:
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