Began implementing abstract methods
parent
88b1a72255
commit
a083313538
22
lib/class.js
22
lib/class.js
|
@ -43,6 +43,28 @@ exports.extend = function( base )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an abstract method
|
||||||
|
*
|
||||||
|
* Abstract methods must be implemented by a subclass and cannot be called
|
||||||
|
* directly. If a class contains a single abstract method, the class itself is
|
||||||
|
* considered to be abstract and cannot be instantiated. It may only be
|
||||||
|
* extended.
|
||||||
|
*
|
||||||
|
* @param {Function} definition function definition that concrete
|
||||||
|
* implementations must follow
|
||||||
|
*
|
||||||
|
* @return {Function}
|
||||||
|
*/
|
||||||
|
exports.abstractMethod = function( definition )
|
||||||
|
{
|
||||||
|
return function()
|
||||||
|
{
|
||||||
|
throw new Error( "Cannot call abstract method" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default class implementation
|
* Default class implementation
|
||||||
*
|
*
|
||||||
|
|
|
@ -66,6 +66,16 @@ var ConcreteFoo = AbstractFoo.extend(
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assert.ok(
|
||||||
|
( abstractMethod() instanceof Function ),
|
||||||
|
"abstractMethod() returns a function"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.throws( function()
|
||||||
|
{
|
||||||
|
abstractMethod()();
|
||||||
|
}, Error, "Abstract methods cannot be called" );
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
( Foo.isAbstract instanceof Function ),
|
( Foo.isAbstract instanceof Function ),
|
||||||
"All classes should have an isAbstract() method"
|
"All classes should have an isAbstract() method"
|
||||||
|
|
Loading…
Reference in New Issue