Understanding delete: some details in javascript

Source: Internet
Author: User

In javascript, we sometimes use delete to delete objects. However, we may not know the details of the delete operation. Yesterday, I saw kangax's article on delete analysis, which benefited a lot. This article will translate the essence of the article and share it with you.

  1. Principle
    1. Code Type
    2. Execution Context
    3. Activate object/variable object
    4. Attribute features
    5. Built-in object and DontDelete
    6. Undeclared value assignment
  2. Firebug confusions
    1. Use eval to delete a variable
  3. Browser compatibility
    1. Gecko DontDelete bug
  4. IE bugs
  5. Misunderstanding
  6. 'Delete' and Host Object
  7. ES5 strict Mode
  8. Summary

Principle

Why can we delete the attributes of an object?

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

However, variables declared like this do not work:

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

Or the declared function:

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

Note: When an attribute cannot be deleted,deleteOnly false is returned.

To understand this, we need to first master concepts such as variable instantiation and attribute features-unfortunately these are rarely mentioned in javascript books. I will try to revisit these concepts in the following sections. It is not difficult to understand them. If you don't care why they are running, you can skip this chapter at will.

Code Type

There are three types of executable code in ECMAScript:Global code),Function code)AndEval code. These types have some self-descriptions, but here is a brief overview:

  1. When a piece of source code body is regarded as a program, it is executed in the global scope and treatedGlobal code). In a browser environment, the content in the SCRIPT element is often parsed as a program. Therefore, it is evaluated as a global code.
  2. Any Code directly executed within a function is obviously treatedFunction code). The content of the event attribute in the browser red (for example:<p onclick="...">) Is usually parsed as Function code;
  3. Finally, the text provided to the built-in function eval () is treatedEval code). We will soon see that this type is very special.
    • 8 pages in total:
    • Previous Page
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Next Page

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.