Skip to content

Commit

Permalink
added article about traits.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Van Cutsem authored and Tom Van Cutsem committed Nov 9, 2010
1 parent dbef73b commit 2a3406a
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 0 deletions.
260 changes: 260 additions & 0 deletions articles/traitsjs.markdown

Large diffs are not rendered by default.

Binary file added articles/traitsjs/1-TMagnitude.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/traitsjs/2-TCircle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/traitsjs/3-Conflicts.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/traitsjs/4-Renaming.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/traitsjs/5-Excluding.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/traitsjs/6-Overriding.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/traitsjs/7-Create.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions articles/traitsjs/example.js
@@ -0,0 +1,54 @@
var Trait = require('traits').Trait;
var print = require('sys').print;

var TEquality = Trait({
equals: Trait.required,
differs: function(x) { return !this.equals(x); }
});

var TMagnitude = Trait.compose(TEquality, Trait({
smaller: Trait.required,
greater: function(x) { return !this.smaller(x) && this.differs(x) },
between: function(min, max) {
return min.smaller(this) && this.smaller(max);
}
}));

function TColor(rgb) {
return Trait.compose(TEquality, Trait({
get rgb() { return rgb; },
equals: function(col) { return col.rgb.equals(this.rgb); }
}));
}

function TCircle(center, radius, rgb) {
return Trait.compose(
TMagnitude,
TEquality,
Trait.resolve({ equals: 'equalColors' }, TColor(rgb)),
Trait({
center: center,
radius: radius,
area: function() { return Math.PI * this.radius * this.radius; },
equals: function(c) { return c.center === this.center &&
r.radius === this.radius },
smaller: function(c) { return this.radius < c.radius }
}));
}

function Circle(center, radius, rgb) {
return Trait.create(Object.prototype,
TCircle(center, radius, rgb));
}

function test() {
var red = {
equals: function(o){ return ''+o === this.toString(); },
toString:function(){return 'red';}};
var c1 = Circle(0, 1, red);
var c2 = Circle(1, 2, red);
print('c1.area: ' + c1.area());
print('\nc1 < c2? ' + c1.smaller(c2));
print('\nc1 !== c2?' + c1.differs(c2));
print('\nc1 equalColors c2? '+c1.equalColors(c2));
}
7 changes: 7 additions & 0 deletions authors/Tom Van Cutsem.markdown
@@ -0,0 +1,7 @@
Github: tvcutsem
Email: tomvc.be@gmail.com
Homepage: http://soft.vub.ac.be/~tvcutsem
Twitter: tvcutsem
Location: Brussels, Belgium

I'm a computer science researcher at the University of Brussels. My focus is on programming language technology. I serve on the ECMAScript technical committee and I'm the author of traits.js, a library for trait composition in Javascript, see http://traitsjs.org

0 comments on commit 2a3406a

Please sign in to comment.