mix v0.0.1
mix.coffee
A simple library for mixing properties into your javascript objects and prototypes. This is similar to the "extend" keyword in ruby land, except notably less awesome.
Installation
This library is available as:
- a node package
npm install -g mixvar Mix = require('mix');
- a rails 3.1 engine gem for use within the asset pipeline ("mix")
gem 'mix'inside your Gemfilerequire mixinside your application.js asset file
- a standalone javascript file (prettified or minified, downloaded through the "Downloads" tab on this page)
Tutorial
Let's imagine that we've created the following classes in coffeescript:
class Coffee
class Beer
class TeaNow, let's imagine that we'd like to create a reusable function for drinking beverages.
Drinkable =
drink: ->
console.log "gulp"We can mix this into the prototype for all of our classes via the Mix method:
Mix(Drinkable).into Coffee.protoype, Beer.prototype, Tea.prototypeNotice that we passed the prototype for our objects to our Mix API.
Now, if we create an instance of any of our classes, we'll be able to drink them:
(new Coffee).drink()Note that we can also extend the Coffee, Beer, and Tea objects with methods directly using the same API:
Alchoholic =
containsAlchohol: ->
true
NonAlchoholic =
containsAlchohol: ->
false
Mix(Alchoholic).into Beer
Mix(NonAlchoholic).into Tea, CoffeeNow, we can ask our objects if they're alchoholic:
Beer.containsAlchohol() #==> true
Tea.containsAlchohol() #==> false
Coffee.containsAlchohol() #==> falseNote that we're not limited to mixing only one object at a time:
Mix(A, B, C).into D, E, F, G14 years ago