js-oop
Version:
jsOOP aids in object oriented programming in JS.
33 lines (23 loc) • 1.25 kB
JavaScript
var Interface = function( descriptor ) {
this.descriptor = descriptor;
};
Interface.prototype.descriptor = null;
Interface.prototype.compare = function( classToCheck ) {
for( var i in this.descriptor ) {
// First we'll check if this property exists on the class
if( classToCheck.prototype[ i ] === undefined ) {
throw 'INTERFACE ERROR: ' + i + ' is not defined in the class';
// Second we'll check that the types expected match
} else if( typeof this.descriptor[ i ] != typeof classToCheck.prototype[ i ] ) {
throw 'INTERFACE ERROR: Interface and class define items of different type for ' + i +
'\ninterface[ ' + i + ' ] == ' + typeof this.descriptor[ i ] +
'\nclass[ ' + i + ' ] == ' + typeof classToCheck.prototype[ i ];
// Third if this property is a function we'll check that they expect the same amount of parameters
} else if( typeof this.descriptor[ i ] == 'function' && classToCheck.prototype[ i ].length != this.descriptor[ i ].length ) {
throw 'INTERFACE ERROR: Interface and class expect a different amount of parameters for the function ' + i +
'\nEXPECTED: ' + this.descriptor[ i ].length +
'\nRECEIVED: ' + classToCheck.prototype[ i ].length;
}
}
};
module.exports = Interface;