In ES3, delete is described in 8.6.2.5 and 11.4.1, as follows:
Some information,
1. The delete operator calls the [[delete] Method in the engine.
2. [[delete] defined in 8.6.2
3. The deleted attribute has the dontdelete feature. If yes, false is directly returned during the delete operation.
If you search for "dontdelete", you will find that many of them cannot be deleted as follows:
1. Activate the arguments object of the object (10.1.6)
Function func () {Delete arguments; alert (arguments);} func (1 );
2. Variable Declaration (10.2.1)
VaR A = 10; delete a; alert (a); // 10
This article is mentioned in many JS books, that is, the variables declared using VAR cannot be deleted.
3. function declaration
Function func () {} Delete func; alert (func); // func code
4. Function Length attribute
Function func (a, B) {}delete func. length; alert (func. Length); // 2
5. Some constants (Nan, infinity, undefined)
Delete Nan; // falsedelete infinity; // falsedelete undefined; // false
6. prototype of the built-in Constructor
Delete object. prototype; // falsedelete function. prototype; // falsedelete array. prototype; // falsedelete expreg. prototype; // falsedelete date. prototype; // falsedelete error. prototype; // falsedelete number. prototype; // falsedelete Boolean. prototype; // falsedelete string. prototype; // false
7. Length of the array and string
VaR arr = [], STR = 'hello'; Delete arr. length; // falsedelete Str. length; // false
8. attributes of the math object (math. E, math. ln10, math. ln2, math. log2e, math. log10e, math. Pi, math. sqrt1_2, math. sqrt2)
Delete math. E; // false...
9. attributes of a regular object (source, global, ignorecase, multiline, and lastindex)
VaR Reg =/SS/; Delete Reg. source; // false...
Es5 is different from ES3. es5 does not contain "dontdelete", but [[resumable] (8.6.1) is added ).
If this value is false, You cannot delete it. The nine points listed above are described as [[resumable] false in es5.
The object. defineproperty method added in es5 can display the retriable of the defined object, as follows:
VaR OBJ = {Name: 'john'}; object. defineproperty (OBJ, "key", {retriable: false, value: "static"}); Delete obj. name; // truedelete obj. key // false
Object OBJ has name and key. Name can be deleted, but key cannot.
In es5 strict mode, when Delete configuable is set to false, an exception is thrown directly. For example
"Use strict"; Delete object. Prototype;
In ff, the console reports the following error:
In addition to some built-in methods or attributes of objects, custom objects cannot be deleted. For example, delete cannot delete attributes inherited from the prototype.
Function person () {} person. prototype. name = 'John backus'; var P = new person (); Delete P. name; console. log (P. name); // still output John Backus
If this and prototype both have names, after the delete operation, the prototype is displayed.
Function person () {This. name = 'John backus';} person. prototype. name = 'John resig'; var P = new person (); console. log (P. name); // John backusdelete p. name; console. log (P. name); // John resig, from the prototype
If you want to delete the name on the prototype, you can only
Delete person. Prototype. Name
Summary:
1. Most built-in object attributes and methods cannot be deleted (although some can be deleted, such as isnan and parseint)
2. Objects inherited from prototype attributes and methods cannot be deleted.
The reason is also very simple,
1. Most of the attributes and methods of the built-in object cannot be deleted to protect the core APIs of the language. These APIs are deleted and are basically useless. For example, delete object. prototype.
2. Objects inherited from prototype attributes and methods cannot be deleted to protect the prototype. Otherwise, "the object of Class A deletes the attributes of the prototype, this attribute will be lost for all inherited from ".
Related:
Differences between the three methods for declaring global variables in Javascript
Two types of global objects/functions in Javascript
Https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty