Mascara JavaScript compiler

Try it Download Buy About Documentation Blog Contact

Getters

Syntax definition [Help]
function get name () [ : type ]
{
  implementation
}

A getter is a function which is invoked the same way that an ordinary variable or property values is read. Example:

function get x() { return Math.random() }
var y = x; 
(y is assigned a random value)

Getters often appear together with setters, but not always. A getter without a setter works like a read-only variable or property.

Getters can be stand-alone or class-members.

Getters can also be declared as part of an object literal, with a slightly different syntax (no function-keyword):

var o = {
   _value: 1001, 
   get value() { return this._value; }
};
var p = o.value;

See also: setters, accessor