summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/Array/from_constructor.js
blob: 6846ef09aaa78b5e23a8cdeb94369272de56c7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/ */

// Array.from can be applied to any constructor.
// For example, the Date builtin constructor.
var d = Array.from.call(Date, ["A", "B"]);
assertEq(Array.isArray(d), false);
assertEq(Object.prototype.toString.call(d), "[object Date]");
assertEq(Object.getPrototypeOf(d), Date.prototype);
assertEq(d.length, 2);
assertEq(d[0], "A");
assertEq(d[1], "B");

// Or Object.
var obj = Array.from.call(Object, []);
assertEq(Array.isArray(obj), false);
assertEq(Object.getPrototypeOf(obj), Object.prototype);
assertEq(Object.getOwnPropertyNames(obj).join(","), "length");
assertEq(obj.length, 0);

// Or any JS function.
function C(arg) {
    this.args = arguments;
}
var c = Array.from.call(C, {length: 1, 0: "zero"});
assertEq(c instanceof C, true);
assertEq(c.args.length, 1);
assertEq(c.args[0], 1);
assertEq(c.length, 1);
assertEq(c[0], "zero");

// If the 'this' value passed to Array.from is not a constructor,
// a plain Array is created.
var arr = [3, 4, 5];
var nonconstructors = [
    {}, Math, Object.getPrototypeOf, undefined, 17,
    () => ({})  // arrow functions are not constructors
];
for (var v of nonconstructors) {
    obj = Array.from.call(v, arr);
    assertEq(Array.isArray(obj), true);
    assertDeepEq(obj, arr);
}

// Array.from does not get confused if global.Array is replaced with another
// constructor.
function NotArray() {
}
var RealArray = Array;
NotArray.from = Array.from;
Array = NotArray;
assertEq(RealArray.from([1]) instanceof RealArray, true);
assertEq(NotArray.from([1]) instanceof NotArray, true);

if (typeof reportCompare === 'function')
    reportCompare(0, 0);