Skip to content

Commit

Permalink
Added mixin support [#1]
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 15, 2009
1 parent 8a78d0d commit b000609
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/oo.js
Expand Up @@ -63,7 +63,13 @@

Class.include = function(props) {
for (var name in props)
if (props.hasOwnProperty(name))
if (name == 'include')
if (props[name] instanceof Array)
for (var i = 0, len = props[name].length; i < len; ++i)
Class.include(props[name][i])
else
Class.include(props[name])
else if (props.hasOwnProperty(name))
prototype[name] =
typeof props[name] == 'function' &&
typeof __super__[name] == 'function' &&
Expand Down
24 changes: 24 additions & 0 deletions spec/spec.core.js
Expand Up @@ -20,6 +20,30 @@ describe 'Class'
end
end

describe '.include'
it 'should include an array of mixins'
Foo = { a: 'b' }
Bar = { c: 'd' }
Baz = Class({
include: [Foo, Bar],
d: 'e'
})
(new Baz).should.have_property 'a', 'b'
(new Baz).should.have_property 'c', 'd'
(new Baz).should.have_property 'd', 'e'
end

it 'should include a single mixin'
Foo = { a: 'b' }
Bar = Class({
include: Foo,
c: 'd'
})
(new Bar).should.have_property 'a', 'b'
(new Bar).should.have_property 'c', 'd'
end
end

describe '.include()'
it 'should merge additional methods'
Foo = Class({
Expand Down

0 comments on commit b000609

Please sign in to comment.