[whatwg/webidl] Intent to use Constants (Issue #1577)

99rd88jc created an issue (whatwg/webidl#1577)

I got redirected here from a chaterbate room don't know why actually...dont know what to do

2.5.1. Constants

A constant is a declaration (matching [Const](https://webidl.spec.whatwg.org/#prod-Const)) used to bind a constant value to a name. Constants can appear on [interfaces](https://webidl.spec.whatwg.org/#dfn-interface) and [callback interfaces](https://webidl.spec.whatwg.org/#dfn-callback-interface).

Constants have in the past primarily been used to define named integer codes in the style of an enumeration. The Web platform is moving away from this design pattern in favor of the use of strings. Editors who wish to use this feature are strongly advised to discuss this by [filing an issue](https://github.com/whatwg/webidl/issues/new?title=Intent%20to%20use%20Constants) before proceeding.

const type constant_identifier = 42;
The [identifier](https://webidl.spec.whatwg.org/#dfn-identifier) of a [constant](https://webidl.spec.whatwg.org/#dfn-constant) must not be the same as the identifier of another [interface member](https://webidl.spec.whatwg.org/#dfn-interface-member) or [callback interface member](https://webidl.spec.whatwg.org/#callback-interface-member) defined on the same [interface](https://webidl.spec.whatwg.org/#dfn-interface) or [callback interface](https://webidl.spec.whatwg.org/#dfn-callback-interface). The identifier also must not be "length", "name" or "prototype".

Note: These three names are the names of properties that are defined on the [interface object](https://webidl.spec.whatwg.org/#dfn-interface-object) in the JavaScript language binding.

The type of a constant (matching [ConstType](https://webidl.spec.whatwg.org/#prod-ConstType)) must not be any type other than a [primitive type](https://webidl.spec.whatwg.org/#dfn-primitive-type). If an [identifier](https://webidl.spec.whatwg.org/#dfn-identifier) is used, it must reference a [typedef](https://webidl.spec.whatwg.org/#dfn-typedef) whose type is a primitive type.

The [ConstValue](https://webidl.spec.whatwg.org/#prod-ConstValue) part of a constant declaration gives the value of the constant, which can be one of the two boolean literal tokens (true and false), an [integer](https://webidl.spec.whatwg.org/#prod-integer) token, a [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token, or one of the three special floating point constant values (-Infinity, Infinity and NaN).

Note: These values – in addition to strings and the empty sequence – can also be used to specify the [default value of a dictionary member](https://webidl.spec.whatwg.org/#dfn-dictionary-member-default-value) or [of an optional argument](https://webidl.spec.whatwg.org/#dfn-optional-argument-default-value). Note that strings, the empty sequence [], and the default dictionary {} cannot be used as the value of a [constant](https://webidl.spec.whatwg.org/#dfn-constant).

The value of the boolean literal tokens true and false are the IDL [boolean](https://webidl.spec.whatwg.org/#idl-boolean) values true and false.

The value of an [integer](https://webidl.spec.whatwg.org/#prod-integer) token is an integer whose value is determined as follows:

Let S be the sequence of [scalar values](https://infra.spec.whatwg.org/#scalar-value) matched by the [integer](https://webidl.spec.whatwg.org/#prod-integer) token.

Let sign be −1 if S begins with U+002D (-), and 1 otherwise.

Let base be the base of the number based on the [scalar values](https://infra.spec.whatwg.org/#scalar-value) that follow the optional leading U+002D (-):

U+0030 (0), U+0058 (X)
U+0030 (0), U+0078 (x)
The base is 16.

U+0030 (0)
The base is 8.

Otherwise
The base is 10.

Let number be the result of interpreting all remaining [scalar values](https://infra.spec.whatwg.org/#scalar-value) following the optional leading U+002D (-) character and any [scalar values](https://infra.spec.whatwg.org/#scalar-value) indicating the base as an integer specified in base base.

Return sign × number.

The type of an [integer](https://webidl.spec.whatwg.org/#prod-integer) token is the same as the type of the constant, dictionary member or optional argument it is being used as the value of. The value of the [integer](https://webidl.spec.whatwg.org/#prod-integer) token must not lie outside the valid range of values for its type, as given in [§ 2.13 Types](https://webidl.spec.whatwg.org/#idl-types).

The value of a [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token is either an IEEE 754 single-precision floating point number or an IEEE 754 double-precision floating point number, depending on the type of the constant, dictionary member or optional argument it is being used as the value for, determined as follows:
Let S be the sequence of [scalar values](https://infra.spec.whatwg.org/#scalar-value) matched by the [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token.

Let result be the Mathematical Value that would be obtained if S were parsed as a JavaScript [NumericLiteral](https://tc39.es/ecma262/#sec-literals-numeric-literals).

If the [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token is being used as the value for a [float](https://webidl.spec.whatwg.org/#idl-float) or [unrestricted float](https://webidl.spec.whatwg.org/#idl-unrestricted-float), then the value of the [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token is the IEEE 754 single-precision floating point number closest to result.

Otherwise, the [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token is being used as the value for a [double](https://webidl.spec.whatwg.org/#idl-double) or [unrestricted double](https://webidl.spec.whatwg.org/#idl-unrestricted-double), and the value of the [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token is the IEEE 754 double-precision floating point number closest to result. [[IEEE-754]](https://webidl.spec.whatwg.org/#biblio-ieee-754)

The value of a constant value specified as Infinity, -Infinity, or NaN is either an IEEE 754 single-precision floating point number or an IEEE 754 double-precision floating point number, depending on the type of the constant, dictionary member, or optional argument it is being used as the value for:

Type [unrestricted float](https://webidl.spec.whatwg.org/#idl-unrestricted-float), constant value Infinity
The value is the IEEE 754 single-precision positive infinity value.

Type [unrestricted double](https://webidl.spec.whatwg.org/#idl-unrestricted-double), constant value Infinity
The value is the IEEE 754 double-precision positive infinity value.

Type [unrestricted float](https://webidl.spec.whatwg.org/#idl-unrestricted-float), constant value -Infinity
The value is the IEEE 754 single-precision negative infinity value.

Type [unrestricted double](https://webidl.spec.whatwg.org/#idl-unrestricted-double), constant value -Infinity
The value is the IEEE 754 double-precision negative infinity value.

Type [unrestricted float](https://webidl.spec.whatwg.org/#idl-unrestricted-float), constant value NaN
The value is the IEEE 754 single-precision NaN value with the bit pattern 0x7fc00000.

Type [unrestricted double](https://webidl.spec.whatwg.org/#idl-unrestricted-double), constant value NaN
The value is the IEEE 754 double-precision NaN value with the bit pattern 0x7ff8000000000000.

The type of a [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token is the same as the type of the constant, dictionary member or optional argument it is being used as the value of. The value of the [decimal](https://webidl.spec.whatwg.org/#prod-decimal) token must not lie outside the valid range of values for its type, as given in [§ 2.13 Types](https://webidl.spec.whatwg.org/#idl-types). Also, Infinity, -Infinity and NaN must not be used as the value of a [float](https://webidl.spec.whatwg.org/#idl-float) or [double](https://webidl.spec.whatwg.org/#idl-double).

The value of the null token is the special null value that is a member of the [nullable types](https://webidl.spec.whatwg.org/#dfn-nullable-type). The type of the null token is the same as the type of the constant, dictionary member or optional argument it is being used as the value of.

If VT is the type of the value assigned to a constant, and DT is the type of the constant, dictionary member or optional argument itself, then these types must be compatible, which is the case if DT and VT are identical, or DT is a [nullable type](https://webidl.spec.whatwg.org/#dfn-nullable-type) whose [inner type](https://webidl.spec.whatwg.org/#dfn-inner-type) is VT.

[Constants](https://webidl.spec.whatwg.org/#dfn-constant) are not associated with particular instances of the [interface](https://webidl.spec.whatwg.org/#dfn-interface) or [callback interface](https://webidl.spec.whatwg.org/#dfn-callback-interface) on which they appear. It is language binding specific whether [constants](https://webidl.spec.whatwg.org/#dfn-constant) are exposed on instances.

The JavaScript language binding does however allow [constants](https://webidl.spec.whatwg.org/#dfn-constant) to be accessed through objects implementing the IDL [interfaces](https://webidl.spec.whatwg.org/#dfn-interface) on which the [constants](https://webidl.spec.whatwg.org/#dfn-constant) are declared. For example, with the following IDL:

[Exposed=Window]
interface A {
  const short rambaldi = 47;
};
the constant value can be accessed in JavaScript either as A.rambaldi or instanceOfA.rambaldi.
The following extended attributes are applicable to constants: [[CrossOriginIsolated](https://webidl.spec.whatwg.org/#CrossOriginIsolated)], [[Exposed](https://webidl.spec.whatwg.org/#Exposed)], and [[SecureContext](https://webidl.spec.whatwg.org/#SecureContext)].

[Const](https://webidl.spec.whatwg.org/#prod-Const) ::
    const [ConstType](https://webidl.spec.whatwg.org/#prod-ConstType) [identifier](https://webidl.spec.whatwg.org/#prod-identifier) = [ConstValue](https://webidl.spec.whatwg.org/#prod-ConstValue) ;
[ConstValue](https://webidl.spec.whatwg.org/#prod-ConstValue) ::
    [BooleanLiteral](https://webidl.spec.whatwg.org/#prod-BooleanLiteral)
    [FloatLiteral](https://webidl.spec.whatwg.org/#prod-FloatLiteral)
    [integer](https://webidl.spec.whatwg.org/#prod-integer)
[BooleanLiteral](https://webidl.spec.whatwg.org/#prod-BooleanLiteral) ::
    true
    false
[FloatLiteral](https://webidl.spec.whatwg.org/#prod-FloatLiteral) ::
    [decimal](https://webidl.spec.whatwg.org/#prod-decimal)
    -Infinity
    Infinity
    NaN
[ConstType](https://webidl.spec.whatwg.org/#prod-ConstType) ::
    [PrimitiveType](https://webidl.spec.whatwg.org/#prod-PrimitiveType)
    [identifier](https://webidl.spec.whatwg.org/#prod-identifier)
The following [IDL fragment](https://webidl.spec.whatwg.org/#dfn-idl-fragment) demonstrates how [constants](https://webidl.spec.whatwg.org/#dfn-constant) of the above types can be defined.

[Exposed=Window]
interface Util {
  const boolean DEBUG = false;
  const octet LF = 10;
  const unsigned long BIT_MASK = 0x0000fc00;
  const double AVOGADRO = 6.022e23;
};
2.5.2. Attributes

An attribute is an [interface member](https://webidl.spec.whatwg.org/#dfn-interface-member) or [namespace member](https://webidl.spec.whatwg.org/#dfn-namespace-member) (matching inherit [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest), static [OptionalReadOnly](https://webidl.spec.whatwg.org/#prod-OptionalReadOnly) [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest), stringifier [OptionalReadOnly](https://webidl.spec.whatwg.org/#prod-OptionalReadOnly) [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest), [OptionalReadOnly](https://webidl.spec.whatwg.org/#prod-OptionalReadOnly) [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest), or [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest)) that is used to declare data fields with a given type and [identifier](https://webidl.spec.whatwg.org/#dfn-identifier) whose value can be retrieved and (in some cases) changed. There are two kinds of attributes:

[regular attributes](https://webidl.spec.whatwg.org/#dfn-regular-attribute), which are those used to declare that objects implementing the [interface](https://webidl.spec.whatwg.org/#dfn-interface) will have a data field member with the given [identifier](https://webidl.spec.whatwg.org/#dfn-identifier)

interface interface_identifier {
  attribute type identifier;
};
[static attributes](https://webidl.spec.whatwg.org/#dfn-static-attribute), which are used to declare attributes that are not associated with a particular object implementing the interface

interface interface_identifier {
  static attribute type identifier;
};
If an attribute has no static keyword, then it declares a regular attribute. Otherwise, it declares a [static attribute](https://webidl.spec.whatwg.org/#dfn-static-attribute). Note that in addition to being [interface members](https://webidl.spec.whatwg.org/#dfn-interface-member), [read only](https://webidl.spec.whatwg.org/#dfn-read-only) [regular attributes](https://webidl.spec.whatwg.org/#dfn-regular-attribute) can be [namespace members](https://webidl.spec.whatwg.org/#dfn-namespace-member) as well.

The getter steps of an attribute attr should be introduced using text of the form “The attr getter steps are:” followed by a list, or “The attr getter steps are to” followed by an inline description.

The setter steps of an attribute attr should be introduced using text of the form “The attr setter steps are:” followed by a list, or “The attr setter steps are to” followed by an inline description.

Note: When defining [getter steps](https://webidl.spec.whatwg.org/#getter-steps), you implicitly have access to [this](https://webidl.spec.whatwg.org/#this). When defining [setter steps](https://webidl.spec.whatwg.org/#setter-steps), you implicitly have access to [this](https://webidl.spec.whatwg.org/#this) and [the given value](https://webidl.spec.whatwg.org/#the-given-value).

The [identifier](https://webidl.spec.whatwg.org/#dfn-identifier) of an [attribute](https://webidl.spec.whatwg.org/#dfn-attribute) must not be the same as the identifier of another [interface member](https://webidl.spec.whatwg.org/#dfn-interface-member) defined on the same [interface](https://webidl.spec.whatwg.org/#dfn-interface). The identifier of a static attribute must not be "prototype".

The type of the attribute is given by the type (matching [Type](https://webidl.spec.whatwg.org/#prod-Type)) that appears after the attribute keyword. If the [Type](https://webidl.spec.whatwg.org/#prod-Type) is an [identifier](https://webidl.spec.whatwg.org/#dfn-identifier) or an identifier followed by ?, then the identifier must identify an [interface](https://webidl.spec.whatwg.org/#dfn-interface), [enumeration](https://webidl.spec.whatwg.org/#dfn-enumeration), [callback function](https://webidl.spec.whatwg.org/#dfn-callback-function), [callback interface](https://webidl.spec.whatwg.org/#dfn-callback-interface) or [typedef](https://webidl.spec.whatwg.org/#dfn-typedef).

The type of the attribute, after resolving typedefs, must not be a [nullable](https://webidl.spec.whatwg.org/#dfn-nullable-type) or non-nullable version of any of the following types:

a [sequence type](https://webidl.spec.whatwg.org/#sequence-type)

an [async sequence type](https://webidl.spec.whatwg.org/#async-sequence-type)

a [dictionary type](https://webidl.spec.whatwg.org/#idl-dictionary)

a [record type](https://webidl.spec.whatwg.org/#record-type)

a [union type](https://webidl.spec.whatwg.org/#dfn-union-type) that has a nullable or non-nullable sequence type, dictionary, or record as one of its [flattened member types](https://webidl.spec.whatwg.org/#dfn-flattened-union-member-types)

The attribute is read only if the readonly keyword is used before the attribute keyword. An object that implements the interface on which a read only attribute is defined will not allow assignment to that attribute. It is language binding specific whether assignment is simply disallowed by the language, ignored or an exception is thrown.

interface interface_identifier {
  readonly attribute type identifier;
};
Attributes whose type is a [promise type](https://webidl.spec.whatwg.org/#dfn-promise-type) must be [read only](https://webidl.spec.whatwg.org/#dfn-read-only). Additionally, they cannot have any of the [extended attributes](https://webidl.spec.whatwg.org/#dfn-extended-attribute) [[LegacyLenientSetter](https://webidl.spec.whatwg.org/#LegacyLenientSetter)], [[PutForwards](https://webidl.spec.whatwg.org/#PutForwards)], [[Replaceable](https://webidl.spec.whatwg.org/#Replaceable)], or [[SameObject](https://webidl.spec.whatwg.org/#SameObject)].

A [regular attribute](https://webidl.spec.whatwg.org/#dfn-regular-attribute) that is not [read only](https://webidl.spec.whatwg.org/#dfn-read-only) can be declared to inherit its getter from an ancestor interface. This can be used to make a read only attribute in an ancestor interface be writable on a derived interface. An attribute [inherits its getter](https://webidl.spec.whatwg.org/#dfn-inherit-getter) if its declaration includes inherit in the declaration. The read only attribute from which the attribute inherits its getter is the attribute with the same identifier on the closest ancestor interface of the one on which the inheriting attribute is defined. The attribute whose getter is being inherited must be of the same type as the inheriting attribute.

Note: The grammar ensures that inherit does not appear on a [read only](https://webidl.spec.whatwg.org/#dfn-read-only) attribute or a [static attribute](https://webidl.spec.whatwg.org/#dfn-static-attribute).

[Exposed=Window]
interface Ancestor {
  readonly attribute TheType theIdentifier;
};

[Exposed=Window]
interface Derived : Ancestor {
  inherit attribute TheType theIdentifier;
};
When the stringifier keyword is used in a [regular attribute](https://webidl.spec.whatwg.org/#dfn-regular-attribute) declaration, it indicates that objects implementing the interface will be stringified to the value of the attribute. See [§ 2.5.5 Stringifiers](https://webidl.spec.whatwg.org/#idl-stringifiers) for details.

interface interface_identifier {
  stringifier attribute DOMString identifier;
};
The following [extended attributes](https://webidl.spec.whatwg.org/#dfn-extended-attribute) are applicable to regular and static attributes: [[CrossOriginIsolated](https://webidl.spec.whatwg.org/#CrossOriginIsolated)], [[Exposed](https://webidl.spec.whatwg.org/#Exposed)], [[SameObject](https://webidl.spec.whatwg.org/#SameObject)], and [[SecureContext](https://webidl.spec.whatwg.org/#SecureContext)].

The following [extended attributes](https://webidl.spec.whatwg.org/#dfn-extended-attribute) are applicable only to regular attributes: [[LegacyLenientSetter](https://webidl.spec.whatwg.org/#LegacyLenientSetter)], [[LegacyLenientThis](https://webidl.spec.whatwg.org/#LegacyLenientThis)], [[PutForwards](https://webidl.spec.whatwg.org/#PutForwards)], [[Replaceable](https://webidl.spec.whatwg.org/#Replaceable)], [[LegacyUnforgeable](https://webidl.spec.whatwg.org/#LegacyUnforgeable)].

[ReadOnlyMember](https://webidl.spec.whatwg.org/#prod-ReadOnlyMember) ::
    readonly [ReadOnlyMemberRest](https://webidl.spec.whatwg.org/#prod-ReadOnlyMemberRest)
[ReadOnlyMemberRest](https://webidl.spec.whatwg.org/#prod-ReadOnlyMemberRest) ::
    [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest)
    [MaplikeRest](https://webidl.spec.whatwg.org/#prod-MaplikeRest)
    [SetlikeRest](https://webidl.spec.whatwg.org/#prod-SetlikeRest)
[ReadWriteAttribute](https://webidl.spec.whatwg.org/#prod-ReadWriteAttribute) ::
    [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest)
[InheritAttribute](https://webidl.spec.whatwg.org/#prod-InheritAttribute) ::
    inherit [AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest)
[AttributeRest](https://webidl.spec.whatwg.org/#prod-AttributeRest) ::
    attribute [TypeWithExtendedAttributes](https://webidl.spec.whatwg.org/#prod-TypeWithExtendedAttributes) [AttributeName](https://webidl.spec.whatwg.org/#prod-AttributeName) ;
[AttributeName](https://webidl.spec.whatwg.org/#prod-AttributeName) ::
    [AttributeNameKeyword](https://webidl.spec.whatwg.org/#prod-AttributeNameKeyword)
    [identifier](https://webidl.spec.whatwg.org/#prod-identifier)
[AttributeNameKeyword](https://webidl.spec.whatwg.org/#prod-AttributeNameKeyword) ::
    required
[OptionalReadOnly](https://webidl.spec.whatwg.org/#prod-OptionalReadOnly) ::
    readonly
    ε
The following [IDL fragment](https://webidl.spec.whatwg.org/#dfn-idl-fragment) demonstrates how [attributes](https://webidl.spec.whatwg.org/#dfn-attribute) can be declared on an [interface](https://webidl.spec.whatwg.org/#dfn-interface):

[Exposed=Window]
interface Animal {

  // A simple attribute that can be set to any string value.
  readonly attribute DOMString name;

  // An attribute whose value can be assigned to.
  attribute unsigned short age;
};

[Exposed=Window]
interface Person : Animal {

  // An attribute whose getter behavior is inherited from Animal, and need not be
  // specified in the description of Person.
  inherit attribute DOMString name;
};

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/webidl/issues/1577
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/webidl/issues/1577@github.com>

Received on Sunday, 8 March 2026 04:34:52 UTC