AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

toJSON

toJSON()方法将Goal对象转换为JSON格式的对象。

语法

toJSON()

返回值

返回JSON格式的对象。

示例

const goal = new Goal("Learn JavaScript", "Strengthen coding skills", 
                       new Date(2022, 11, 31), 10, true);
const goalJSON = goal.toJSON();
console.log(goalJSON);

输出结果:

{
  "title": "Learn JavaScript",
  "description": "Strengthen coding skills",
  "dueDate": "2022-12-31T08:00:00.000Z",
  "priority": 10,
  "completed": true
}

备注

该方法只能用于Goal对象。如果要转换其他类型的对象,请使用内置的JSON.stringify()方法。

示例代码

class Goal {
  constructor(title, description, dueDate, priority, completed) {
    this.title = title;
    this.description = description;
    this.dueDate = dueDate;
    this.priority = priority;
    this.completed = completed;
  }
  
  toJSON() {
    return {
      title: this.title,
      description: this.description,
      dueDate: this.dueDate.toISOString(),
      priority: this.priority,
      completed: this.completed
    };
  }
}

const goal = new Goal("Learn JavaScript", "Strengthen coding skills", 
                       new Date(2022, 11, 31), 10, true);
const goalJSON = goal.toJSON();
console.log(goalJSON);