javascript item splice self out of list

前端 未结 5 1531
慢半拍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:39

    I would suggest creating an object/class that represents the active enemies list. Create methods on that instance for adding/removing a given item from the list - abstracting the inner workings of the data structure from the outside world. If the active enemies list is global (e.g. there's only one of them), then you can just reference it directly to call the remove function when you die. If it's not global, then you'll have to give each item a reference to the list so it can call the function to remove itself.

提交回复
热议问题