Skip to content

Commit e68590c

Browse files
committed
Make PointObj iterable and accessible like an array.
1 parent 1a85cac commit e68590c

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/BasicCanvas.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,27 @@ class PointObj {
236236
}
237237
}
238238

239-
get point() {
240-
return this;
241-
}
242-
243-
get array() {
244-
return [this.x, this.y];
239+
get 0() { return this.x; }
240+
get 1() { return this.y; }
241+
set 0(x) { this.x = x; }
242+
set 1(y) { this.y = y; }
243+
244+
[Symbol.iterator]() {
245+
const array = [this.x, this.y];
246+
let i = 0;
247+
248+
return {
249+
next() {
250+
const it = { value: array[i], done: i >= 2 };
251+
i += 1;
252+
return it;
253+
}
254+
};
245255
}
246256

247-
set array(a) {
248-
[this.x, this.y] = a;
249-
}
257+
get point() { return this; }
258+
get array() { return [this.x, this.y]; }
259+
set array(a) { [this.x, this.y] = a; }
250260

251261
floor() {
252262
return new PointObj(Math.floor(this.x), Math.floor(this.y));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "basiccanvas",
44
"title": "BasicCanvas",
55
"description": "Simple JavaScript canvas abstractions.",
6-
"version": "1.3.2",
6+
"version": "1.3.3",
77
"main": "lib/BasicCanvas.js",
88
"homepage": "https://github.com/Demonstrandum/BasicCanvas/",
99
"author": {

0 commit comments

Comments
 (0)