| 1 |
// This file was procedurally generated from the following sources: |
| 2 |
// - src/class-fields/private-names.case |
| 3 |
// - src/class-fields/productions/cls-expr-new-sc-line-generator.template |
| 4 |
/*--- |
| 5 |
description: static literal private names (field definitions followed by a method in a new line with a semicolon) |
| 6 |
esid: prod-FieldDefinition |
| 7 |
features: [class-fields-private, class, class-fields-public, generators] |
| 8 |
flags: [generated] |
| 9 |
includes: [propertyHelper.js] |
| 10 |
info: | |
| 11 |
ClassElement: |
| 12 |
... |
| 13 |
FieldDefinition ; |
| 14 |
|
| 15 |
FieldDefinition: |
| 16 |
ClassElementName Initializer_opt |
| 17 |
|
| 18 |
ClassElementName: |
| 19 |
PrivateName |
| 20 |
|
| 21 |
PrivateName: |
| 22 |
#IdentifierName |
| 23 |
|
| 24 |
---*/ |
| 25 |
|
| 26 |
|
| 27 |
var C = class { |
| 28 |
#x; #y; |
| 29 |
*m() { return 42; } |
| 30 |
x() { |
| 31 |
this.#x = 42; |
| 32 |
return this.#x; |
| 33 |
} |
| 34 |
y() { |
| 35 |
this.#y = 43; |
| 36 |
return this.#y; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
var c = new C(); |
| 41 |
|
| 42 |
assert.sameValue(c.m().next().value, 42); |
| 43 |
assert.sameValue(c.m, C.prototype.m); |
| 44 |
assert.sameValue(Object.hasOwnProperty.call(c, "m"), false); |
| 45 |
|
| 46 |
verifyProperty(C.prototype, "m", { |
| 47 |
enumerable: false, |
| 48 |
configurable: true, |
| 49 |
writable: true, |
| 50 |
}); |
| 51 |
|
| 52 |
// Test the private fields do not appear as properties before set to value |
| 53 |
assert.sameValue(Object.hasOwnProperty.call(C.prototype, "#x"), false, "test 1"); |
| 54 |
assert.sameValue(Object.hasOwnProperty.call(C, "#x"), false, "test 2"); |
| 55 |
assert.sameValue(Object.hasOwnProperty.call(c, "#x"), false, "test 3"); |
| 56 |
|
| 57 |
assert.sameValue(Object.hasOwnProperty.call(C.prototype, "#y"), false, "test 4"); |
| 58 |
assert.sameValue(Object.hasOwnProperty.call(C, "#y"), false, "test 5"); |
| 59 |
assert.sameValue(Object.hasOwnProperty.call(c, "#y"), false, "test 6"); |
| 60 |
|
| 61 |
// Test if private fields can be sucessfully accessed and set to value |
| 62 |
assert.sameValue(c.x(), 42, "test 7"); |
| 63 |
assert.sameValue(c.y(), 43, "test 8"); |
| 64 |
|
| 65 |
// Test the private fields do not appear as properties before after set to value |
| 66 |
assert.sameValue(Object.hasOwnProperty.call(c, "#x"), false, "test 9"); |
| 67 |
assert.sameValue(Object.hasOwnProperty.call(c, "#y"), false, "test 10"); |