The Delete in JavaScript

Source: Internet
Author: User

In this article, the author starts with the error about delete in theJavaScript Object-oriented Programming Guide , detailing the implementation of the delete operation, its limitations, and the performance in different browsers and plugins (referred to as Firebug).

The following translation is the main part.

... The book claims

"The function is like a normal variable-it can be copied to different variables and even deleted"

and attach the following code snippet as a description:

var function (A, B) {return a + b;}; var add = sum; Delete sum; true // test, display false in Chrome typeof sum;" Undefined

Can you find the problem in the fragment? The problem is that the operation to delete the sum variable should not be successful; the delete declaration should not return true and typeof sum should not return to undefined. Because a variable cannot be deleted in JavaScript, at least it cannot be declared in this way.

So what happened to this example? Is it a print error or a joke? I don't think so. This fragment is an actual output from the Firebug console, and Stoyan (the author who wrote the story above) should be the quick test done with it. This seems to indicate that Firebug has some different deletion rules. It was firebug that misled stoyan!. So what the hell is going on in there?

To answer this question, we need to understand how the delete operator works in Javascript: which can be deleted, which cannot be deleted, and why. I'll try to explain the details below. We will realize that it is not really "strange" by observing Firebug's "strange" behavior; we will dig deeper into those that are hidden behind the details when we declare variables, functions, assignment properties, and delete them; we will look at the implementation of this browser and some famous bugs; We will also discuss the strict mode in ECMAScript version 5 (strict mode) and how it alters the behavior of the delete operator.

The Javascript and ecmpscript I use interchangeably are generally referred to as ECMAScript (except when explicitly talking about the JavaScript™ implementation of Mozilla).

Unsurprisingly, there is currently very little explanation for delete on the Web (author Press: This article was written in January 2010). The resources of the MDC (MDN]) are probably the most detailed of these, but unfortunately it misses some interesting details that include the firebug of the above. The MSDN documentation is almost useless.

First, theory | Theory

So, why can we delete the properties of an object:

var x = {a:1 }; Delete // true // undefined

But you cannot delete a variable:

var x = 1; Delete // false; // 1

You cannot delete a function:

function X () {}; Delete // false;
typeof X; "Function"

Note: Delete returns false only if a property cannot be deleted .

To understand this, we need to first grasp some concepts: variable instantiation (variable instantiation) and intrinsic properties of properties (property attributes) (Translator Press: About property and attributes The differences are referred to in the reference articles, which are intended to be translated into internal properties according to the following: these are rarely mentioned in JavaScript books. In the following paragraphs I will try to briefly review these things, and it is not difficult to understand them. If you are not concerned about the reasons behind their performance, you can skip this chapter.

1.1. Type of code | Type of code

There are three types of executable code in ECMAScript:

    1. Global Code
    2. function code
    3. Eval Code

The meanings of these classes are roughly as they are named, but they are quick to review:

    1. When a source file is viewed as a program, it executes within the global scope (scope), which is considered a global code. In a browser environment, the contents of a SCRIPT element are usually parsed into a program and thus executed as a global code.

    2. Of course, any code executed directly in a function is considered a function code, in the context of the browser, the contents of the event properties (e.g. <a onclick="...") are usually parsed and executed as function code.

    3. Finally, the code that is put into the built-in function eval is parsed as an Eval code. We will soon see why this type is special.

1.2. Context of Code Execution | Execution Context

When the ECMAScript code executes, it always occurs in a deterministic execution context (the context). The execution scope is an abstract entity that helps to understand how scopes and variable instantiation work. The above three types of executable code have their own execution context. When the function code executes, we say that the control has entered the execution context of the function code, and when the global code executes, we say that the control has entered the execution of the global Code, and so on.

As you can see, the execution context is logically a stack (stack). First, there may be a global code that has its own execution context, a function that can be called in this code, which also has its own execution context, which may call another function, and so on. Even when a function calls itself recursively, it enters a different execution context in each step of the call.

1.3. Activating objects and Variable objects | Activation Object/variable Object

Each execution context has a variable object associated with it (Variable object). Like it, a variable object is also an abstract entity, a mechanism used to describe the instantiation of a variable. Interestingly, the variables and functions declared in a piece of source code are actually added to the variable object as a Variable object (the properties) of the Variable objects .

When control enters the execution context of the global Code, a global object is used as the variable object. This is precisely why the globally declared variables and functions become properties of a global object :

The Delete in JavaScript

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.