javascript item splice self out of list

前端 未结 5 1414
慢半拍i
慢半拍i 2021-02-20 11:46

If I have an array of objects is there any way possible for the item to splice itself out of the array that contains it?

For example: If a bad guy dies he will splice hi

5条回答
  •  北海茫月
    2021-02-20 12:17

    Assuming the bad guy knows what list he's in, why not?

    BadGuy.prototype.die = function()
    {
        activeEnemies.splice(activeEnemies.indexOf(this), 1);
    }
    

    By the way, for older browsers to use indexOf on Arrays, you'll need to add it manually.

提交回复
热议问题