JavaScript Tutorial: Delete Delete Object

Source: Internet
Author: User
Tags object eval variable

In JavaScript, we sometimes use Delete to delete objects. However, we may not know some details about the delete. Yesterday, the article that saw Kangax analysis Delete, benefited greatly. This article will be the essence of the article translated to share with you.

    1. Principle
      1. Code type
      2. Execution context
      3. Activate object/mutable object
      4. Attribute attributes
      5. Built-in objects and Dontdelete
      6. Undeclared assignment
    2. Firebug confused
      1. To delete a variable through eval
    3. Browser compatibility
      1. Gecko Dontdelete Bugs
    4. IE bugs
    5. Misunderstanding
    6. ' Delete ' and host object
    7. ES5 Strict mode
    8. Summarize

Principle

Why can we delete an object's properties?

var o = { x: 1 };
delete o.x; // true
o.x; // undefined

However, a variable declared like this will not work:

var x = 1;
delete x; // false
x; // 1

Or a function so declared:

function x(){}
delete x; // false
typeof x; // "function"

Note that when a property cannot be deleted, delete only false is returned.

To understand this, we first need to grasp concepts such as variable instantiation and attribute attributes-which, sadly, are rarely mentioned in the book on JavaScript. I will try to revisit these concepts briefly in the next few paragraphs. It's not hard to understand them, and if you don't care why they're running, you can skip this chapter at random.

Code type

There are three types of executable code in ECMAScript: Global Code, function code , and Evalcode. These types have a bit of self description, but here's a short overview:

    1. When a piece of source code body is considered a program, it executes in the global scope and is treated as Global code. In a browser environment, the content in a script element is usually interpreted as a program, so it is evaluated as a global code.
    2. Any code that executes directly inside a function is clearly treated as a function code. The contents of event attributes (such as:) in the browser red <p onclick="..."> are usually parsed as functional codes (function code);
    3. Finally, the text that is provided to the built-in function eval () is parsed as an eval code . We will soon see that this type is very special.



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.