Mascara Try it Download Buy About Documentation Blog

Accessors

Accessors are the common name for getters and setters. Getters and setters are functions that are invoked by getting or setting a property. See getter or setter for the specific syntax.

An example with both a getter or setter:

class EcoStorage {
  var _internalValue = 0
  function set value(val) { 
      _internalValue / 2; 
  }
  function get value() { 
     return _internalValue * 2; 
  }
}
  
var storage = new EcoStorage;
storage.value = 1001;
print(storage.value);
This class saves memory by only saving half the value, but this behavior is encapsulated using accessor. For the user of the object, value just looks like an ordinary property.