mozilla

getter

The get syntax binds an object property to a function that will be called when that property is looked up.

Syntax

{get prop() { ... } }
{get [expression]() { ... } }

Parameters

prop
The name of the property to bind to the given function.
expression
Starting with ECMAScript 6, you can also use expressions for a computed property name to bind to the given function.

Description

Sometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want 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.

Note the following when working with the get syntax:

A getter can be removed using the delete operator.

Examples

Defining a getter on new objects in object initializers

This will create a pseudo-property latest for object obj, which will return the last array item in log.

var log = ['test'];
var obj = {
  get latest () {
    if (log.length == 0) return undefined;
    return log[log.length - 1]
  }
}
console.log (obj.latest); // Will return "test".

Note that attempting to assign a value to latest will not change it.

Deleting a getter using the delete operator

If you want to remove the getter, you can just delete it:

delete obj.latest;

Defining a getter on existing objects using defineProperty

To append a getter to an existing object later at any time, use Object.defineProperty().

var o = { a:0 }

Object.defineProperty(o, "b", { get: function () { return this.a + 1; } });

console.log(o.b) // Runs the getter, which yields a + 1 (which is 1)

Using a computed property name

Note: Computed properties are experimental technology, part of the ECMAScript 6 proposal, and are not widely supported by browsers yet. This will trigger a syntax error in non-supporting environments.

var expr = "foo";

var obj = {
  get [expr]() { return "bar"; }
};

console.log(obj.foo); // "bar"

Specifications

Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'Object Initializer' in that specification.
Standard Initial definition.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Method definitions' in that specification.
Standard Added computed property names.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1 2.0 (1.8.1) 9 9.5 3
Computed property names Not supported 34 (34) Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) 1.0 (1.8.1) (Yes) (Yes) (Yes)
Computed property names Not supported Not supported 34.0 (34.0) Not supported Not supported Not supported

See also

Document Tags and Contributors

Last updated by: fscholz,
Hide Sidebar