JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript delete有关的电子书:

4.13.3 The delete Operator

4.13.3 The delete Operator


delete is a unary operator that attempts to delete the object property or array element specified as its operand.1 Like the assignment, increment, and decrement operators, delete is typically used for its property deletion side effect, and not for the value it returns. Some examples:

var o = { x: 1, y: 2}; // Start with an object delete o.x; // Delete one of its properties "x" in o // => false: the property does not exist anymore

var a = [1,2,3]; // Start with an array delete a[2]; // Delete the last element of the array a.length // => 2: array only has two elements now

Note that a deleted property or array element is not merely set to the undefined value. When a property is deleted, the property ceases to exist. Attempting to read a nonexistent property returns undefined, but you can test for the actual existence of a property with the in operator ( §4.9.3 ).

delete expects its operand to be an lvalue. If it is not an lvalue, the operator takes no action and returns true. Otherwise, delete attempts to delete the specified lvalue. delete returns true if it successfully deletes the specified lvalue. Not all properties can be deleted, however: some built-in core and client-side properties are immune from deletion, and user-defined variables declared with the varstatement cannot be deleted. Functions defined with the function statement and declared function parameters cannot be deleted either.

In ECMAScript 5 strict mode, deleteraises a SyntaxError if its operand is an unqualified identifier such as a variable, function, or function parameter: it only works when the operand is a property access expression ( §4.4 ). Strict mode also specifies that delete raises a TypeError if asked to delete any nonconfigurable property (see §6.7 ). Outside of strict mode, no exception occurs in these cases and delete simply returns false to indicate that the operand could not be deleted.

Here are some example uses of the delete operator:

var o = {x:1, y:2}; // Define a variable; initialize it to an object

delete o.x; // Delete one of the object properties; returns true

typeof o.x; // Property does not exist; returns "undefined"

delete o.x; // Delete a nonexistent property; returns true

delete o; // Can't delete a declared variable; returns false.

// Would raise an exception in strict mode.

delete 1; // Argument is not an lvalue: returns true

this.x = 1; // Define a property of the a global object without var

delete x; // Try to delete it: returns true in non-strict mode

1. If you are a C++ programmer, note that the delete keyword in JavaScript is nothing like the delete keyword in C++. In JavaScript, memory deallocation is handled automatically by garbage collection, and you never have to worry about explicitly freeing up memory. Thus, there is no need for a C++-style delete to delete entire objects.

// Exception in strict mode. Use 'delete this.x' instead x; // Runtime error: x is not defined

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27826.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1