Properties for JavaScript. Prototype level, class based.
JavaScript CoffeeScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Properties for JavaScript Prototypes.

Library provides class based properties that can be bound directly to a prototype, which reduces redundancy of coping the same property among instances.

Additionally, we can easily inherit from Properties class, extending it by our own getters/setter logic.

Default property's value is inited in a lazy way, first time it's accessed.


{ property } = require 'jsprops'

class Klass
    foo: property 'foo'
    bar: property('bar', null, 'def_value')
    baz: property('baz',
        set: (set, val) ->
            set val.replace /a/, 'b'
    woof: property('woof',
        init: (set) -> set null
        get: (get) -> get().replace /z/, 'b'
        set: (set, val) ->
            set val.replace /a/, 'b'

instance = new Klass # returns 'foo' 'bar' # returns 'bar'

Repeated name in a declaration is required by the design.


Signals are extended properties, adding nice bindings to an event emitter. An interesting feature is the support for inheritance. You can define an overriding signal in a child class, while still preserving listener defined in the super class. The only condition is the requirement of using @signal instead of signal (needed for a prototype chain resolution).

For signals, set is an emit and get returns temp object with binding functions (on, once) and an init function. All of them can be defined inside a declaration. They are also compatible with EventEmitter2Async (with callback and after/before events). Additionally, each signal needs to be initialized (manually or using SignalsMixin#initSignals).


} = require 'jsprops'

# This example actually lacks event emitter, but mixins are out of it's scope.
class Klass extends SignalsMixin
    foo: signal('foo',
        on: -> console.log 'klass1'

    on: -> # forward to a composed event emitter
    emit: -> # forward to a composed event emitter

class Klass2 extends Klass
    constructor: ->

    foo: @signal('foo',
        on: -> console.log 'klass2'

instance = new Klass ->
    console.log 'listener' ->
    # prints 'klass1', 'klass2', 'listener'

JavaScript examples

property = require('jsprops').property

function Klass() {} = property('foo') = property('bar', null, 'def_value')
Klass.prototype.baz = property('baz', {
    set: function(set, val) {
        set(val.replace(/a/, 'b'))
Klass.prototype.baz = property('woof', {
        init: function(set) {
        get: function(get) {
            return get().replace(/z/, 'b')
        set: (set, val) ->
            set(val.replace(/a/, 'b'))
    }, 'def_value'


npm install jsprops


This library provides contracts (read - runtime type checking) based on library. There's a special build for that, so don't worry when using the default one. To make it work, a flag global.contracts is needed.