Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make property attributes of getter, setter, method syntax explicit #30947

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions files/en-us/web/javascript/reference/functions/get/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ There are some additional syntax restrictions:

## Description

Sometimes it is desirable to allow access to a property that returns a dynamically
computed value, or you may want to reflect the status of an internal variable without
requiring the use of explicit method calls. In JavaScript, this can be accomplished with
the use of a _getter_.

It is not possible to simultaneously have a getter bound to a property and have that
property actually hold a value, although it _is_ possible to use a getter and a
setter in conjunction to create a type of pseudo-property.
Sometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want to reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a _getter_.

An object property is either a data property or an accessor property, but it cannot simultaneously be both. Read {{jsxref("Object.defineProperty()")}} for more information. The getter syntax allows you to specify the getter function in an object initializer.

```js
const obj = {
get prop() {
// getter, the code executed when reading obj.prop
return someValue;
},
};
```

Properties defined using this syntax are own properties of the created object, and they are configurable and enumerable.

## Examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const obj = {
};
```

Properties defined using this syntax are own properties of the created object, and they are configurable, enumerable, and writable, just like normal properties.

[`function*`](/en-US/docs/Web/JavaScript/Reference/Statements/function*), [`async function`](/en-US/docs/Web/JavaScript/Reference/Statements/async_function), and [`async function*`](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) properties all have their respective method syntaxes; see examples below.

However, note that the method syntax is not equivalent to a normal property with a function as its value — there are semantic differences. This makes methods defined in object literals more consistent with methods in [classes](/en-US/docs/Web/JavaScript/Reference/Classes).
Expand Down
17 changes: 13 additions & 4 deletions files/en-us/web/javascript/reference/functions/set/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ There are some additional syntax restrictions:

## Description

In JavaScript, a setter can be used to execute a function whenever a specified property
is attempted to be changed. Setters are most often used in conjunction with getters to
create a type of pseudo-property. It is not possible to simultaneously have a setter on
a property that holds an actual value.
In JavaScript, a setter can be used to execute a function whenever an attempt is made to change a property's value. Setters are most often used in conjunction with getters.

An object property is either a data property or an accessor property, but it cannot simultaneously be both. Read {{jsxref("Object.defineProperty()")}} for more information. The setter syntax allows you to specify the setter function in an object initializer.

```js
const obj = {
set prop() {
// setter, the code executed when setting obj.prop
},
}
```

Properties defined using this syntax are own properties of the created object, and they are configurable and enumerable.

## Examples

Expand Down