Current location - Music Encyclopedia - Earning online - How to delete an object
How to delete an object
This paper introduces the method of deleting middle key of Javascript object, and then mainly discusses the usage of delete in detail, which is very detailed for your reference.

The copy code code is as follows:

Delete thisIsObject[key]

or

Delete thisIsObject.key

By the way, the usage of delete.

A few weeks ago, I had the opportunity to read the book Object-oriented Javascript by Stoian Stefanov.

This book has a high rating on Amazon (12 review, 5 stars), so I was curious to see if it was such a recommended book, so I began to read the chapter on functions.

I really appreciate the way this book explains things. These examples are organized in a very beautiful and gradual way. It seems that even beginners can easily master this knowledge. however

Almost immediately, I found an interesting misunderstanding that runs through the whole chapter-deleting functional functions. There are some other errors (such as the difference between function declaration and function expression).

But it is not discussed at present.

This book claims:

A function is treated as an ordinary variable-it can be copied into different variables and even deleted. This explanation is accompanied by an example:

The copy code code is as follows:

var sum = function(a,b){ return a+b; }

var add = sum

Delete sum

real

Type of sum;

"Undefined"

Ignore some missing semicolons, can you see what's wrong with these codes? Obviously, the error is that deleting the variable sum will not succeed.

Delete expression should not return true, and typeof sum should not return "undefined".

All this is because it is impossible to delete variables in JavaScript. At least, it is impossible to declare in this way.

So, what happened in this example? Is this a mistake? Or a special usage? Probably not.

This code is actually the real output in the Firebug console, and Stoyan must use it as a quick test tool.

It's as if Firebug obeyed some other deletion rules. Fireflies led Stoian astray! What the hell happened here?

Before answering this question, we first need to understand how the delete operator in JavaScript works: what can be deleted,

What can't be deleted Today I will try to explain this problem in detail. We will study Firebug's "strange" behavior and realize that it is not that strange.

We will learn more about the things behind the scenes of declaring variables, functions, assigning values to properties and deleting them. We will look at browser compatibility and some of the most notorious errors.

We will also discuss the strict mode of ES5 and how it changes the behavior of the delete operator.

I will use JavaScript and ECMAScript interchangeably, both of which mean ECMAScript (unless you explicitly refer to Mozilla's JavaScript implementation).

Unsurprisingly, on the Internet, the explanation of delete is quite scarce. MDC articles may be the easiest resource to understand, but unfortunately,

It leaves out some interesting details of the subject. Strangely, one thing that has been forgotten is the reason why Firebug behaves strangely. And MSDN

These references are almost useless.

theory

So, why can we delete an object's attribute:

The copy code code is as follows:

var o = { x: 1 };

Delete o.x// true

O.x// undefined

You cannot delete an object declared like this:

The copy code code is as follows:

var x = 1;

Delete x; //False

x; // 1

Or a function:

The copy code code is as follows:

Function x () {

Delete x; //False

Type of x; //"function"

Note: When an attribute cannot be deleted, the delete operator will only return false.

To understand this, we first need to master these concepts about variable instances and attribute characteristics-unfortunately, these concepts are rarely mentioned in JavaScript books.

I will briefly review these concepts in the next few paragraphs.

These concepts are hard to understand! If you don't care why these things are like this, skip this chapter.

Code type:

In ECMAScript, there are three different types of executable code: global code, function code and Eval code. These types are more or less self-evident in their names. Here is a brief overview:

When a piece of source code is regarded as a program, it will be executed in the global environment and regarded as a global code. In the browser environment, the content of a script element is usually interpreted as a program, so it is executed as global code.

Any code executed directly in a function is obviously considered as function code. In the browser, the contents of the event attribute (such as

Finally, the code text applied to the built-in function Eval is interpreted as eval code. We will soon find out why this type is special.

Execution context:

When executing ECMAScript code, it usually happens in a specific execution context. Execution context is a somewhat abstract entity concept.

It helps to understand how scope and variable instantiation work. For each of the three executable codes,

When a function is executed, we say that "program control has entered the execution context of the function code"; When a piece of global code is executed,

Program control has entered the execution of global code.

-

Online earning platform Qt: 60 1 183 Presenter: 3 groups of cattle people.