Ruby inspired Structs for node and the browser.
$ npm install strukt
var Struct = require'strukt';
Browser: Grab lib/strukt.js and include it in your HTML document.
Struct function will be added. AMD and CommonJS loaders are also supported.
Simple. Pass the parameters you want to
Struct, a new constructor function will be returned.
var Point = 'x' 'y';var p = 42 1337;px; // 42py; // 1337
Struct 'x''y'# More class logicp = 421337;px # 42py # 1337
Explicit is better than implicit. The Zen of Python
Explicits structs are pretty much the same thing, but you pass an object to the returned constructor.
var Person = 'forename' 'surname';var john =forename: 'John'surname: 'Doe';johnforename; // 'John'johnsurname; // 'Doe'
Maybe you'd like to add a constructor function to your Struct. If the last argument of
Struct.Explicit is a function it'll used as a constructor function.
var Person = 'forename' 'surname'alert'Welcome ' + thisforename + " " + thissurname;;forename: 'John'surname: 'Doe'; // Will alert 'Welcome John Doe'
Struct.Explicit return functions, so you can just add properties to the prototype as always.
var Point = "x" "y";return thisx == ptx && thisy == pty;
Visit spec/index.html in your browser to run the test suite.
Alternatively you can install testacular and enjoy the awesomeness
$ testacular start
It will launch several browsers and run the tests again if a a spec or the library code changes.