About JavaScript Medium delete operation

See a question , That's true :

(function(x){
delete x;
return x;
})(1);
  • 1

  • null

  • undefined

  • Error

    I fast Error、、、、, But it's wrong , Run it for a while , The answer is 1.

    Can't you delete Parameters in a function ? What can be delete Of ? What kind of situation can't be delete Of ?

    I went to the Internet to check

    Usually :delete The operator is used to delete the attributes of an object .

    grammar :delete expression

    If successful return true, Failure to return false, But in strict mode , If the property is not configurable ,delete It throws an exception .

    If delete Operator deleted successfully , The deleted attribute will disappear completely from the object it belongs to .

"use strict"
var obj={
name:"aa"
}
Object.defineProperty(obj, "name", { writable: true,
enumerable: true,
configurable: false
});
delete obj.name;
console.log(obj.name);

obj.name After setting to non configurable delete, In strict mode, the following exception is thrown

Uncaught TypeError: Cannot delete property 'name' of #<Object>

delete Operators can also be used to delete implicitly declared global variables , It just doesn't work var Declared global variables

There was a problem in the experiment

 var name="aa";
delete name;
console.log(name);

and

 var name1="aa";
delete name1;
console.log(name1);

The runtime is different , In the first example , Will report a mistake . because name It's a built-in property 、、、

And marked DontDelete Some of the object's properties cannot be delete.

/* foo It's normally declared , So with DontDelete Mark , So it can't be deleted ! */
var foo = 1;
delete foo; // false
console.log(typeof foo); // "number"
/* bar Is declared as a function , Same with DontDelete, Cannot be deleted . */
function bar(){}
delete bar; // false
console.log(typeof bar);// "function" /* Function var The variable of , Same with DontDelete, Cannot be deleted .*/
function f() {
var z = 44;
delete z; // returns false
console.log(z);//44
}

It should be noted that some built-in objects hold this tag automatically , So it can't be deleted , For example, in a function arguments, And functions length attribute , And the incoming Parameters .

If the variable is delete 了 , If you take it again, you will report an error ,index.html:11 Uncaught ReferenceError: x is not defined,x( A variable ) Can not find .

About JavaScript Medium delete More articles on operation

  1. JavaScript Use in ActiveXObject How to operate a local folder

    Reprinted address     http://www.jb51.net/article/48538.htm stay Windows On the platform , js You can call a lot of Windows Provided ActivexObject, This article uses js Lai Shi ...

  2. Javascript Medium delete

    One . Problem presentation Let's take a look at the following code first , It should be noted that , The following code is not in the browser's developer tool ( Such as FireBug.Chrome Developer tool) Run in , The reason will be explained later : Why can we delete objects ...

  3. javascript In the string commonly used operation arrangement

    javascript In the string commonly used operation arrangement String operation in js Very often , And very important . I used to remember it very clearly after reading books , But not at intervals , You'll forget about it , Bad memory is a hard injury ... Today, I'm going to talk about some common strings ...

  4. Javascript Medium delete Introduce

    About JavaScript Medium Delete I haven't made it very clear , I've read two articles on this recently , Now I will translate some of the contents of the two articles ( There are changes and additions to the content , The order is not exactly the same , I'd like to recommend it ), I hope it can help you One . The problem of ...

  5. In depth understanding of javascript Medium delete( turn )

    In this article, the author starts from <JavaScript Object oriented programming guide > A book about delete From the mistakes of , It's about delete Implementation of operations , Limitations as well as in different browsers and plug-ins ( Here it means firebu ...

  6. javascript Array operations in

    1. Array creation var arrayObj = new Array(); // Create an array var arrayObj = new Array([size]); // Create an array and specify the length , Note that it's not the upper limit , ...

  7. [ turn ] JavaScript String operations in

    One . summary     The string is in JavaScript Almost everywhere in , When you process user input data , In reading or setting DOM Object's properties , In operation cookie when , Of course, there are more many ....JavaScript The core part of provides ...

  8. In depth understanding of JavaScript Function operation in ——《JavaScript Ninja Secret 》 summary

    Anonymous functions For what is an anonymous function , I won't give you too much introduction here . What we need to know is , about JavaScript for , Anonymous function is a very important and logical property . Usually , The use of anonymous functions is : Create a function for later use . ordinary ...

  9. javascript Summary of common string operations in 、JS String operation encyclopedia

    String operation in js Very often , And very important . I used to remember it very clearly after reading books , But not at intervals , You'll forget about it , Bad memory is a hard injury ... Today, I will sort out some common operations of string , One makes the impression , Both are convenient for the future ...

Random recommendation

  1. 2-MySQL Database coding uft-8

    mysql> show variables like 'character%'; mysql> show variables like 'collation%'; mysql> st ...

  2. Compare the major mobile portals Taobao 、 JD.COM 、 NetEase 、 Sina 、 tencent meta The similarities and differences of labels

    First of all, let's show the information about each website meta Code of label : NetEase <meta charset="UTF-8"> <meta content="width=devi ...

  3. BZOJ2226: [Spoj 5971] LCMSum

    Answer key : Consider enumeration gcd, And then the problem turns into asking for <=n And with the n The sum of the reciprocal numbers . There is a formula f[i]=phi[i]*i/2 And then you can go through when you get a card . Code : #include<cstdio> # ...

  4. A little understanding MySQL Character set

    Although the purpose of this article is to understand . But if you know something about it . After all, Zhuge Kongming is like this       Only character based values have the concept of character sets       Some character sets may require more CPU. Consume more memory and disk space . Even affect the use of index       That doesn't include ...

  5. spring aop in pointcut Expression complete

    spring aop in pointcut Expression complete This paper mainly introduces spring aop in 9 Write a pointcut expression execute within this target args @target @with ...

  6. Golang Go Go Go part1: Installation and operation

    golang  Knowledge map https://www.processon.com/view/link/5a9ba4c8e4b0a9d22eb3bdf0 One . install The latest version of the installation package address :https://gola ...

  7. @ControllerAdvice Use of annotations

    package com.vcredit.ddcash.monitor.controller; import com.vcredit.ddcash.monitor.model.dto.Response; ...

  8. Netty Learning materials

    Netty Learning materials Netty Learning materials Link website explain Netty 4.x User guide http://wiki.jikexueyuan.com/project/netty-4-user-guide/ ...

  9. Oracle PLSQL Read ( analysis )Excel file

    http://www.itpub.net/thread-1921612-1-1.html !!!https://code.google.com/p/plsql-utils/ Introduction Medium ...

  10. Android Studio In the project , Which documents should be ignored and not submitted to svn On the server ?

    Android Studio The files recommended for filtering in : - .idea  Folder - .gradle  Folder - be-all  build  Folder - be-all  .iml  file - local.properties  ...