Permalink
Browse files

* reademe.textile: added readme.

  • Loading branch information...
1 parent 57cfa6c commit a1b685aaf202dc5ef58f9aad11ce993a00401dd0 David Nolen committed May 18, 2010
Showing with 56 additions and 0 deletions.
  1. +56 −0 reademe.textile
View
@@ -0,0 +1,56 @@
+h1. Datatype Mixin for Underscore.js
+
+Initializing class-like things in JavaScript is a big pain. Also writing your objects in the following manner is also tedious:
+
+<pre>
+var MyClass = function() {
+ this.foo = "foo";
+ this.bar = "bar";
+};
+
+MyClass.prototype.baz = function() {
+ ...
+}
+
+MyClass.prototype.woz = function() {
+ ...
+}
+</pre>
+
+Using datatype.js you can instead write the following:
+
+<pre>
+var MyClass = _.datatype({
+ initialize: function() {
+ this.foo = "foo";
+ this.bar = "bar";
+ },
+
+ baz: function () {
+ },
+
+ woz: function() {
+ }
+});
+</pre>
+
+datatype.js doesn't support datatype inheritance of any kind. However it is sometimes useful to reuse a name. For this purpose there is <code>_.multi</code>. You can use this like so:
+
+<pre>
+var Foo = _.datatype({});
+var Bar = _.datatype({});
+
+_.multi("cool", Foo, function(x) {
+ console.log("got type Foo!");
+});
+
+_.multi("cool", Foobar, function(x) {
+ console.log("got type Bar!");
+});
+
+var a = new Foo();
+var b = new Bar();
+
+_.cool(a);
+_.cool(b);
+</pre>

0 comments on commit a1b685a

Please sign in to comment.