# property-accessors

> A mixin for declaring property accessors

Latest version **1.1.3** (published 2015-02-13) · 0 weekly downloads

## Install

```sh
npm install property-accessors
pnpm add property-accessors
yarn add property-accessors
bun add property-accessors
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2015-02-13 |
| First published | 2013-12-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Nathan Sobo |
| Maintainers | nathansobo, kevinsawicki, benogle |
| Keywords | property, accessors, metaprogramming |

## Links

- npm: https://www.npmjs.com/package/property-accessors
- Repository: https://github.com/atom/property-accessors
- Homepage: http://atom.github.io/property-accessors/
- Issues: https://github.com/atom/property-accessors/issues
- npm.io page: https://npm.io/package/property-accessors

## Dependencies (2)

- [mixto](https://npm.io/package/mixto.md) 1.x
- [es6-weak-map](https://npm.io/package/es6-weak-map.md) ^0.1.2

## Recent versions

- 1.1.3 (latest) — 2015-02-13
- 1.1.2 — 2015-02-11
- 1.1.1 — 2015-02-11
- 1.1.0 — 2014-03-18
- 1.0.0 — 2014-01-16
- 0.1.0 — 2013-12-07

## README

# Property Accessors Mixin [![Build Status](https://travis-ci.org/atom/property-accessors.svg?branch=master)](https://travis-ci.org/atom/property-accessors)

A mixin for defining dynamic properties.

## Basic Usage

To define a basic property accessor, use the `accessor` declaration. If you've
included the mixin into a class, you define a prototype property by calling
`@::accessor` on its prototype.

```coffee
PropertyAccessors = require 'property-accessors'

class Vehicle
  PropertyAccessors.includeInto(this)

  @::accessor 'type',
    get: ->
      switch @doorCount
        when 4 then 'sedan' # i know this isn't strictly accurate
        when 2 then 'coupe'
    set: (type) ->
      switch type
        when 'sedan' then @doorCount = 4
        when 'coupe' then @doorCount = 2

car = new Vehicle
car.doorCount = 2
car.type # => 'coupe'
```

You can define a class-level property by *extending* with the mixin rather than
including it (which extends the prototype).

```coffee
class Vehicle
  PropertyAccessors.extend(this)

  @accessor 'vehicleCount', get: -> @allVehicles.length  
```

You can just pass a single function if you only want to define a getter:

```coffee
class Vehicle
  PropertyAccessors.includeInto(this)

  @::accessor 'type', -> # ...
```

## Fancy Usage

### Lazy Accessors

Lazy accessors call a function the first time a property is accessed. You are
still free to overwrite this value by assigning the property explicitly.

```coffee
class ScienceLab
  PropertyAccessors.includeInto(this)

  @::lazyAccessor 'crazyComputation', -> computeCrazyComputation()
```

### Advised Accessors

Advised accessors allow you to call code before the reading or writing of a
property value. If a property is being assigned, your advice function is called
with the value being assigned and the old value.

```coffee
class SpyStation
  @advisedAccessor 'online',
    get: -> @ensureAllSystemsNominal()
    set: -> @ensureUserIsSpy()

station = new SpyStation
station.online = true # ensures user is a spy, then assigns true
station.online # ensures all systems are nominal, then returns true
```

---
_Source: https://npm.io/package/property-accessors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
