2
I tried something like:
function Objeto(){
this.del = function(){
delete this;
}
}
var a = new Objeto();
a.del();
But the variable a is still existing
I know the method quoted in the @bfavaretto response, but for the code I am working on I cannot 'move up a level' in the variable structure to run this delete, or obj = null;
This is the section where I want to use this:
Bullet.prototype.step = function() {
for (var i = 0; i < mobs.length; i++){
if ((this.x >= mobs[i].x && this.x <= mobs[i].x + mobs[i].size) && (this.y >= mobs[i].y && this.y <= mobs[i].y + mobs[i].size)){
mobs[i].getHit(this.damage);
delete this;
}
}
};
Or the complete reference:http://codepen.io/GabrielMaia/pen/LEmROB
Gabriel, the problem is that the
deletedoes not delete the object but a property of an object. If you test:window.teste = new Object(); delete window.teste;, the reference to thetestebut if someone has a reference for it, it is possible to access it.– Wakim
Can you explain better why you’re wanting to do this?
– bfavaretto
It is the basic of a projectile system in a simple game... When a Bullet contact a mob it should disappear, because when many of these are loaded the loss of performance is visible... I intend to make sure that when finding a mob, or the boundaries of the screen, Bullet is destroyed, giving way to a new Bullet in the array Bullets
– Gabriel Maia
So all that needs to be done is remove that bullet from the array. Create a function for this, which receives the bullet in question and remove the corresponding position from the array. You can find the position with
indexOf, and remove withsplice.– bfavaretto
It Works the/ .
– Gabriel Maia
OK @Gabrielmaia, good that it worked! I updated my answer to concentrate the information in one place.
– bfavaretto