In the project, some objects need to be reset after use, the following is a brief introduction to the next JS in the method of clearing objects. Here's how:
Method One: Literal definition object
The first step is to define an empty object and print out the code and effect:
Code:
var student = {};
Console.log (student);
Printing results:
The second step is to add properties to the object and print the code and print the results as follows:
Code:
Student.name = "Xiaoming";
Student.age = 12;
Console.log (student);
Printing results:
In the third step, delete the object properties (empty objects) and print, and the code and printing results are as follows:
Code:
for (var key in student) {
Delete Student[key];
}
Console.log (student);
Printing results:
This shows that after the object is emptied, it becomes an empty object, which implements the object reset.
All JS code:
Execution Result:
Method Two: Constructor definition object
Code:
Printing results:
JS empty object \ Delete object's properties