What kind of mysticism in the work of the JS function?

There is a fragment of my code (app.js):
enemy2 = scene.addSprite('enemy.png', 710, 710, 100, 100);

if(game.vIsDown)
{
scene.destroySprite(enemy2);
 }


Everything is simple, click the button object is deleted. Now let us turn to plug-in library with this method.
1 Varint
destroySprite(sprite)
{
 var index_ = this.gameObjects.indexOf(sprite);

 if(index_ != -1)
{
 this.gameObjects.splice(index_, 1); // Action is performed
}
else
 warn(index_ == -1); // index_ set to -1, this should not be
 }

Option 2
destroySprite(sprite)
{
 var index_ = this.gameObjects.indexOf(sprite);

 console.log(index_); // index_ equal to 2, everything is as it should be
 }


The problem appears only after checking with If - else
June 14th 19 at 18:59
1 answer
June 14th 19 at 19:01
If you have
index_ is set to 2, everything is as it should be

it
if(index_ != -1)
{
 this.gameObjects.splice(index_, 1); // Action is performed
 }

as time runs

Find more questions by tags JavaScript