nodejs object_to_json

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-09-27 01:00:00 +00:00
parent b3401b61ed
commit 89593b97c2
3 changed files with 93 additions and 24 deletions

View File

@@ -0,0 +1,47 @@
#!/usr/bin/env node
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
class MyClassSub {
constructor(a, b) {
this.a = a;
this.b = b;
}
}
class MyClass {
constructor(a, b, sub) {
this.a = a;
this.b = b;
this.sub = sub;
}
}
console.log(JSON.stringify(new MyClass(1, 2, new MyClassSub(3, 4))));
class MyClassCustomSub {
constructor(a, b) {
this.a = a;
this.b = b;
}
toJSON() {
return {
a: this.a,
}
}
}
class MyClassCustom {
constructor(a, b, sub) {
this.a = a;
this.sub = sub;
}
toJSON() {
return {
a: this.a,
sub: this.sub,
}
}
}
console.log(JSON.stringify(new MyClassCustom(1, 2, new MyClassCustomSub(3, 4))));