JavaScript Delete delete using sample code

Source: Internet
Author: User
Tags eval

The code is as follows:
var flower={};
Flower.name= "OOP";
Delete Flower.name; True
alert (flower.name); Undefined

Create an object named Flower
Flower has member name, value "OOP";
Delete operation deletes this member
Deletion succeeded, no longer exists flower.name this member
JavaScript Tutorial Delete Example 2
Copy code code as follows:
Alert (isNaN (5)); False
Delete isNaN; True
Alert (isNaN (5)); Undefined

The delete operator can even delete members of global object global
Cannot delete variable for VAR declaration
Copy code code as follows:
var flower= "Monkey";
Delete flower; False
alert (flower); "Monkey"

Variable declared with Var, delete returns false. And did not delete the success variable still exists;
Note: Delete returns false only if you delete a member that cannot be deleted
Under IE cannot delete the variables under the host object under IE browser
Copy code code as follows:
Window.flower= "Monkey";
Delete flower; Throw an exception
alert (flower);

Under the FF browser
Copy code code as follows:
Window.flower= "Monkey";
Delete flower; True
Alert (flower)//undefined

Browser performance is inconsistent when you see members of the Delete window
Window is the host object of JavaScript
The host object can be defined by the JavaScript execution environment itself
In the Ie6-8 browser, you cannot delete window.flower, and the browser prompts you to "object does not support the operation", that is, you cannot delete the members of the window
You cannot delete a function that is declared with a function name
Copy code code as follows:
function Flower () {}
Delete flower; True
alert (flower);//undefined

The result shows that delete cannot delete a function declared with a function name
You cannot delete a member that inherits from a prototype
Copy code code as follows:
function Flower () {};
Flower.prototype.name= "Monkey";
var a1=new flower ();
A1.name= "A1_monkey"
alert (a1.name);//"A1_monkey"
Delete A1.name;//ture
alert (a1.name);//"Monkey"

A1 is an instance of flower, and it is not feasible to delete the prototypes and the members of the parent class through an instance.
If you must delete the attribute ("Here for name example"), you can only manipulate the prototype
Delete A1.constructor.prototype.name;
Dontdelete attribute Delete cannot delete a member with a Dontdelete attribute
So what is a member with a dontdelete feature?
For example, Var affirms the variable, function name stated function, length of function object and so on very few have dontdelete characteristic
Delete Returns a value of false or True
Delete returns false only if you delete a member that cannot be deleted
In other cases, deleting a member that does not exist, or the deletion succeeds, will return ture
This means that returning true does not necessarily mean that the deletion succeeded
such as: Execute code alert (delete a); True
A is a variable that is not declared and does not exist. The delete still returns true
Differences between different browsers
Copy code code as follows:
(function () {
Delete arguments; False, and returns true in Mozilla
typeof arguments; "Object"
})();

Ingenious use of Eval to delete var declaration variables
Copy code code as follows:
Eval (' var flower = 1 ');
Alert (window.flower)//1
Alert (flower)//1
Delete flower; True
alert (flower); "Undefined"
var a=function () {};
Eval (' var a = function () {} ');
Delete A; True
alert (a); "Undefined"

The

Eval global variable becomes dontdelete and can be deleted with eval;
Finally add a magical ~ to sleep just before testing
Window.flower=1; object does not support
Delete Flower
We can use with (window) {flower=1}; then delete flower (remember delete flower, not delete window.flower , IE is not allowed to do so)
so the window.flower is deleted

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.