toJSON
是 CompositeGoal
类的方法之一。该方法将 CompositeGoal
对象转换为 JSON 格式的字符串。 CompositeGoal
表示具有多个子目标(即 Goal
对象)的目标。使用 toJSON
方法可以方便地将 CompositeGoal
对象保存在本地或传输到服务器。
CompositeGoal.toJSON()
该方法不接受参数。
返回一个 JSON 字符串,其中包含 CompositeGoal
对象的所有成员变量及其对应的值。
const goal1 = new Goal("goal1", true);
const goal2 = new Goal("goal2", true);
const goal3 = new Goal("goal3", false);
const compositeGoal = new CompositeGoal("compositeGoal", [goal1, goal2, goal3]);
console.log(compositeGoal.toJSON());
{
"name": "compositeGoal",
"completed": false,
"subGoals": [
{
"name": "goal1",
"completed": true
},
{
"name": "goal2",
"completed": true
},
{
"name": "goal3",
"completed": false
}
]
}
该方法不会抛出异常。
CompositeGoal
的 toJSON
方法只会将直接子节点转换为 JSON 格式的字符串,不会递归转换其子节点的子节点。如果需要递归转换,请使用递归函数。
无。