MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1let passed = 0;
2let failed = 0;
3
4function test(name, actual, expected) {
5 if (actual === expected) {
6 console.log("PASS:", name);
7 passed++;
8 return;
9 }
10 console.log("FAIL:", name, "- expected", expected, "got", actual);
11 failed++;
12}
13
14const ctx = { total: 0, seen: "" };
15[1, 2, 3].forEach(function (n) {
16 this.total += n;
17 this.seen += String(n);
18}, ctx);
19test("forEach thisArg", ctx.total, 6);
20test("forEach callback this binding", ctx.seen, "123");
21
22const mapped = [1, 2].map(function (n) {
23 return this.base + n;
24}, { base: 10 });
25test("map thisArg", mapped[0], 11);
26test("map thisArg second value", mapped[1], 12);
27
28const found = [1, 2, 3].find(function (n) {
29 return n > this.min;
30}, { min: 2 });
31test("find thisArg", found, 3);
32
33console.log("Passed:", passed);
34console.log("Failed:", failed);
35
36if (failed > 0) throw new Error("test_array_callback_thisarg failed");