Is emptying objects akin to destroying them? Is destroying of objects easier than creating them? Let’s find out.
Initiate an object.
|
|
Reset value
|
|
The above piece of code will reset apple
object.
Upon initiation memory was allocated to the defined object. Then, the variable apple
was made to refer to the specific memory location where the object was stored.
By resetting the value of apple
, you are creating a new memory location with an empty object and effectively pointing the variable apple
to that location. The older memory has to be managed by the garbage collector.. Also, you will not be able to do this if you use const
to declare apple
.
For arrays: you could similarly do ..
|
|
The consequences are same as that of an object.
Empty Responsibly
You could take things in your own hands, by demolishing the object in place and rebuilding it.
|
|
Empty Arrays
To empty arrays, you can do the following -
|
|
Personally, I have been using the short hand notation and am super comfortable leaving the job of clearing memory to the garbage collector.