toJSON
是Yuka js库的状态(State)类中的一个方法,其作用是将当前状态对象转换为JSON格式。
state.toJSON();
返回当前状态对象转换为的JSON格式数据。
import { State } from 'yuka';
class MyState extends State {
constructor() {
super();
this.hp = 100;
this.mp = 50;
}
}
const state = new MyState();
const json = state.toJSON();
console.log(json); // {"hp":100,"mp":50}
toJSON
方法不接收任何参数。toJSON
方法会将状态对象中的所有可枚举属性(默认情况下不会枚举原型链上的属性)转换为JSON格式数据,并返回该数据。