mirror of
https://github.com/bellard/quickjs.git
synced 2024-11-22 21:58:12 +08:00
ed3b3516a7
It is probably not trivial to guess that classes created like in the point example do not have enumerable properties (at least it was not for #315). Therefore PointEnumerable class describes how to implement such class.
46 lines
905 B
JavaScript
46 lines
905 B
JavaScript
/* example of JS module importing a C module */
|
|
import { Point } from "./point.so";
|
|
import { PointEnumerable } from "./point.so";
|
|
|
|
function assert(b, str)
|
|
{
|
|
if (b) {
|
|
return;
|
|
} else {
|
|
throw Error("assertion failed: " + str);
|
|
}
|
|
}
|
|
|
|
class ColorPoint extends Point {
|
|
constructor(x, y, color) {
|
|
super(x, y);
|
|
this.color = color;
|
|
}
|
|
get_color() {
|
|
return this.color;
|
|
}
|
|
};
|
|
|
|
function main()
|
|
{
|
|
var pt, pt2, pt3;
|
|
|
|
pt = new Point(2, 3);
|
|
assert(pt.x === 2);
|
|
assert(pt.y === 3);
|
|
pt.x = 4;
|
|
assert(pt.x === 4);
|
|
assert(pt.norm() == 5);
|
|
|
|
pt2 = new ColorPoint(2, 3, 0xffffff);
|
|
assert(pt2.x === 2);
|
|
assert(pt2.color === 0xffffff);
|
|
assert(pt2.get_color() === 0xffffff);
|
|
|
|
pt3 = new PointEnumerable(2, 3);
|
|
assert(JSON.stringify(pt) === "{}");
|
|
assert(JSON.stringify(pt3) === "{\"x\":2,\"y\":3}");
|
|
}
|
|
|
|
main();
|