This proposal adds syntactic support for per-instance private object state by means of private field declarations within class bodies.
See the proposal repository for background material and discussion.
class Point {
#x;
#y;
constructor(x = 0, y = 0) {
#x = +x;
#y = +y;
}
get x() { return #x }
set x(value) { #x = +value }
get y() { return #y }
set y(value) { #y = +value }
equals(p) { return #x === p.#x && #y === p.#y }
toString() { return `Point<${ #x },${ #y }>` }
}
private
or pri
keyword; see the GitHub thread. Another proposed addition is an abbreviated comma syntax, of the form pri x, y
.
\
"constructor"
, return a new empty With parameter homeObject.
With parameter functionObject.
With parameter className.
constructor(... args){ super (...args);}
using the syntactic grammar with the constructor( ){ }
using the syntactic grammar with the "derived"
."constructor"
, F).The [[Construct]] internal method for an ECMAScript Function object F is called with parameters argumentsList and newTarget. argumentsList is a possibly empty
"base"
, then"%ObjectPrototype%"
)."base"
, then"base"
, return super
KeywordThis algorithm has been modified to inline the abstract operation
#x
) which have an implicit this
as the receiver, as well as explicit receivers (obj.#x
). The implicit receiver form is intended for better ergonomics for the common case, and the explicit receiver form allows the full generality of "class-private" (as opposed to "instance-private").
delete
OperatorThese static rules have been modified to produce an delete
operator is applied to a private reference.
It is a Syntax Error if the derived
and
The last rule means that expressions such as delete (((foo)))
produce early errors because of recursive application of the first rule.
When a delete
operator occurs within delete
operator occurs within
The Reference type is used to explain the behaviour of such operators as delete
, typeof
, the assignment operators, the super
keyword and other language features. For example, the left-hand operand of an assignment is expected to produce a reference.
A Reference is a resolved name or property binding. A Reference consists of four components, the base value, the referenced name, the Boolean valued strict reference flag, and the Boolean valued private reference flag. The base value is either
A
The following abstract operations are used in this specification to access the components of references:
The following abstract operations are used in this specification to operate on references:
The object that may be created in step 5.a.ii is not accessible outside of the above abstract operation and the ordinary object [[Get]] internal method. An implementation might choose to avoid the actual creation of the object.
The object that may be created in step 6.a.ii is not accessible outside of the above algorithm and the ordinary object [[Set]] internal method. An implementation might choose to avoid the actual creation of that object.
Component | Purpose |
---|---|
LexicalEnvironment |
Identifies the |
VariableEnvironment |
Identifies the |
PrivateFieldEnvironment |
Identifies the |
"#"
.The Private Field Identifier specification type is used to describe a globally unique identifier which represents a private field name. A private field identifier may be installed on any ECMAScript object with the
All ECMAScript objects have a new additional internal slot, [[PrivateFieldValues]], which is a
Private fields are designed to have semantics analogous to WeakMaps. However, the implied garbage collection semantics are weaker: If all the references to a WeakMap are inaccessible, but there is still a reference to a key which was in the WeakMap, one would expect the value to be eventually collected. However, PrivateFieldIdentifiers specifically do not have this connotation: because the reference from the Identifier to the Value is in a
Private Field Identifiers are a specification type here, not directly observable to ECMAScript code. However, in a decorator integration strawman, an object wrapping Private Field Identifiers would be exposed to allow greater metaprogramming.
The abstract operation ObjectCreate with argument proto (an object or null) is used to specify the runtime creation of new ordinary objects. The optional argument internalSlotsList is a