OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-computed-property-names --harmony-object-literals | |
6 | |
7 | |
8 function ID(x) { | |
9 return x; | |
10 } | |
11 | |
12 | |
13 (function TestMethodComputedNameString() { | |
14 var object = { | |
15 a() { return 'A'}, | |
16 ['b']() { return 'B'; }, | |
17 c() { return 'C'; }, | |
18 [ID('d')]() { return 'D'; }, | |
19 }; | |
20 assertEquals('A', object.a()); | |
21 assertEquals('B', object.b()); | |
22 assertEquals('C', object.c()); | |
23 assertEquals('D', object.d()); | |
24 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); | |
25 })(); | |
26 | |
27 | |
28 (function TestMethodComputedNameNumber() { | |
29 var object = { | |
30 a() { return 'A'; }, | |
31 [1]() { return 'B'; }, | |
32 c() { return 'C'; }, | |
33 [ID(2)]() { return 'D'; }, | |
34 }; | |
35 assertEquals('A', object.a()); | |
36 assertEquals('B', object[1]()); | |
37 assertEquals('C', object.c()); | |
38 assertEquals('D', object[2]()); | |
39 // Array indexes first. | |
40 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object)); | |
41 })(); | |
42 | |
43 | |
44 (function TestMethodComputedNameSymbol() { | |
45 var sym1 = Symbol(); | |
46 var sym2 = Symbol(); | |
47 var object = { | |
48 a() { return 'A'; }, | |
49 [sym1]() { return 'B'; }, | |
50 c() { return 'C'; }, | |
51 [ID(sym2)]() { return 'D'; }, | |
52 }; | |
53 assertEquals('A', object.a()); | |
54 assertEquals('B', object[sym1]()); | |
55 assertEquals('C', object.c()); | |
56 assertEquals('D', object[sym2]()); | |
57 assertArrayEquals(['a', 'c'], Object.keys(object)); | |
58 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(object)); | |
59 })(); | |
60 | |
61 | |
62 function assertIteratorResult(value, done, result) { | |
63 assertEquals({ value: value, done: done}, result); | |
64 } | |
65 | |
66 | |
67 (function TestGeneratorComputedName() { | |
68 var object = { | |
69 *['a']() { | |
70 yield 1; | |
71 yield 2; | |
72 } | |
73 }; | |
74 var iter = object.a(); | |
75 assertIteratorResult(1, false, iter.next()); | |
76 assertIteratorResult(2, false, iter.next()); | |
77 assertIteratorResult(undefined, true, iter.next()); | |
78 assertArrayEquals(['a'], Object.keys(object)); | |
79 })(); | |
80 | |
81 | |
82 (function TestToNameSideEffects() { | |
83 var counter = 0; | |
84 var key1 = { | |
85 toString: function() { | |
86 assertEquals(0, counter++); | |
87 return 'b'; | |
88 } | |
89 }; | |
90 var key2 = { | |
91 toString: function() { | |
92 assertEquals(1, counter++); | |
93 return 'd'; | |
94 } | |
95 }; | |
96 var object = { | |
97 a() { return 'A'; }, | |
98 [key1]() { return 'B'; }, | |
99 c() { return 'C'; }, | |
100 [key2]() { return 'D'; }, | |
101 }; | |
102 assertEquals(2, counter); | |
103 assertEquals('A', object.a()); | |
104 assertEquals('B', object.b()); | |
105 assertEquals('C', object.c()); | |
106 assertEquals('D', object.d()); | |
107 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); | |
108 })(); | |
109 | |
110 | |
111 (function TestDuplicateKeys() { | |
112 'use strict'; | |
113 // ES5 does not allow duplicate keys. | |
114 // ES6 does but we haven't changed our code yet. | |
115 | |
116 var object = { | |
117 a() { return 1; }, | |
118 ['a']() { return 2; }, | |
119 }; | |
120 assertEquals(2, object.a()); | |
121 })(); | |
OLD | NEW |