]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | function shouldBe(actual, expected) { |
2 | if (actual !== expected) | |
3 | throw new Error('bad value: ' + actual); | |
4 | } | |
5 | ||
6 | for (var i = 0; i < 10; ++i) { | |
7 | Object.defineProperty(Array.prototype, i, { | |
8 | get() { | |
9 | throw new Error('get is called.'); | |
10 | }, | |
11 | set(value) { | |
12 | throw new Error('set is called.'); | |
13 | } | |
14 | }); | |
15 | } | |
16 | ||
17 | class ArrayLike { | |
18 | constructor(length) { | |
19 | this.lengthCalled = false; | |
20 | this._length = length; | |
21 | } | |
22 | set length(value) { | |
23 | this.lengthCalled = true; | |
24 | this._length = value; | |
25 | } | |
26 | get length() { | |
27 | return this._length; | |
28 | } | |
29 | } | |
30 | ||
31 | var arrayLike = new ArrayLike(10); | |
32 | for (var i = 0; i < 10; ++i) { | |
33 | arrayLike[i] = i; | |
34 | } | |
35 | shouldBe(arrayLike.lengthCalled, false); | |
36 | ||
37 | var generated = Array.from.call(ArrayLike, arrayLike); | |
38 | ||
39 | shouldBe(generated.length, 10); | |
40 | shouldBe(generated instanceof ArrayLike, true); | |
41 | for (var i = 0; i < 10; ++i) { | |
42 | shouldBe(generated[i], i); | |
43 | } | |
44 | shouldBe(arrayLike.lengthCalled, false); | |
45 | shouldBe(generated.lengthCalled, true); |