How come JSON can't save object's functions?

前端 未结 2 889
走了就别回头了
走了就别回头了 2021-01-05 20:02

In my game, I save the current state by converting all the objects to JSON and then saving that to a file. Some objects, like enemies, have functions on them, but JSON can\'

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 20:34

    I think that you have to save the type on your object so that the functions can be re-added at parsing time. E.g. put a type property on your enemy in the constructor. At parsing time, first parse the string like normal JSON and then deep-traverse the resulting object. When you encounter something that was an Enemy, re-attach the methods or so.

提交回复
热议问题