Get the App
SLTechnology News&Howtos  ›  Development  › 

The function of delete keyword in js

Shulou Source: shulou.com Published: 2022-06-03 16:02:55 09月22日 Update

This article introduces the relevant knowledge of "the role of delete keywords in js". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Delete is the keyword. Used to delete the properties of an object. Syntax: the deleteobject.propertydeleteobject ['property'] delete operator removes the specified property from an object. True is returned when the deletion is successful, otherwise false is returned.

What the delete keyword does:

Delete object attribute syntax: delete object. Attribute

You can delete global variables that are not declared with the var keyword (attributes defined directly on top of window)

Note when using the delete keyword:

The return value type is a Boolean (true/false), and true is returned after deletion.

Delete attributes that do not exist in the object, and the return value is true

Delete the property in the prototype object of the object (cannot be deleted), and the return value is true

Global variables declared with var cannot be deleted, but attributes defined directly on window can be deleted.

Example of delete keyword:

one

2vardog = {

3name: "recruiting money"

4age:14

5}

6move / delete the attributes of the object

7console.log (deletedog.name); / / true

8console.log (dog.name); / / undefined

nine

10Universe / Delete attributes that do not exist in the object

11console.log (deletedog.eat); / / true

twelve

13Universe / deletes attributes in the prototype object of the object

14Object.prototype.play= "play"

15console.log (deleteObject.prototype.play); / / true

sixteen

If there is no variable declared with the var keyword, the variable becomes a global variable by default (becomes a property of window)

18b = "testb"

19console.log (b); / / testb

twenty

21functionfunc () {

22varnum1=10

23num2=20

24}

twenty-five

26func ()

27//console.log (num1); / / error cannot access local variables outside the function body

28console.log (num2); / / 20num2 becomes an attribute of window

twenty-nine

30max / use delete to delete attributes defined directly on window

31console.log (deletenum2); / / true

32//console.log (num2); / / error report

thirty-three

34vara = "testa"

35deleteaterranking / cannot be deleted

36console.log (a); / / testa

thirty-seven

thirty-eight

This is the end of the content of "the role of delete keywords in js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Attributes objects keywords keywords variables functions global faceted success content prototype more knowledge grammar practicality learning next function dilemma reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology OPPO Reno Redmi MySQL