Create a gist now

Instantly share code, notes, and snippets.

@azer /gist:4136102
Last active Oct 13, 2015

What would you like to do?
AdaJS OOP Example
var ada = require('ada'),
assert = require('assert');
function Person(firstName, lastName) {
var obj = ada({
firstName: firstName,
lastName: lastName
obj.fullName = ada(obj.firstName, obj.lastName, function(first, last){ // This is both a binding and a computable property. It'll be called when firstName and/or lastname changed for once and the returned value will be cached.
return first + ' ' + last;
obj.onChange = ada(obj.firstName, obj.lastName); // publish all changes on firstName and lastName to the subscribers of onChange.
return obj;
var john = Person('John', 'Lennon');
john.onChange(function(firstName, lastName){
console.log('John has been updated: ', firstName, lastName);
assert.equal( john.fullName(), 'John Lennon');
john.firstName('John Winston'); // sets name as "John Winston"
assert.equal( john.fullName(), 'John Winston Lennon');
function Speaker(firstName, lastName, company){ // extends Person
var obj = Person.apply(null, arguments),
fullName = obj.fullName; // this will be overrided below. = ak47(company);
obj.fullName = ak47(fullName,, function(fullName, company){
return fullName + ', ' + company;
return obj;
var douglas = Speaker('Douglas', 'Crockford', 'Yahoo');
douglas.onChange(function(firstName, lastName, company){
console.log('Douglas has been updated: ', firstName, lastName, company);
assert.equal( douglas.fullName(), 'Douglas Crockford, Yahoo');
assert.equal( douglas.fullName(), 'Douglas Crockford, Paypal');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment