// Require the core node modules. var chalk = require( "chalk" ); // ----------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------- // function BaseClass() { // .... }; BaseClass.prototype = { // No instance methods... }; BaseClass.isBaseClass = function( value ) { return( value instanceof BaseClass ); }; // ----------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------- // function SubClass() { BaseClass.call( this ); } // Inherit instance methods from base class. SubClass.prototype = Object.create( BaseClass.prototype ); // Inherit static methods. // -- // NOTE: In ES6, the static methods are inherited automatically; however, in ES5, // extending the "class" prototype doesn't affect the inheritance chain of the // constructor function itself. As such, we have to manually copy static methods from // the base constructor into the sub constructor. for ( var key in BaseClass ) { if ( BaseClass.hasOwnProperty( key ) && ( typeof( BaseClass[ key ] ) === "function" ) ) { SubClass[ key ] = BaseClass[ key ]; } } SubClass.isSubClass = function( value ) { return( value instanceof SubClass ); }; // ----------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------- // console.log( chalk.red.bold( "SubClass.isBaseClass:" ), SubClass.isBaseClass ); console.log( chalk.red.bold( "SubClass.isSubClass:" ), SubClass.isSubClass ); var b = new BaseClass(); var s = new SubClass(); console.log( chalk.cyan( "SubClass.isBaseClass( b ):" ), SubClass.isBaseClass( b ) ); console.log( chalk.cyan( "SubClass.isSubClass( b ):" ), SubClass.isSubClass( b ) ); console.log( chalk.cyan( "SubClass.isBaseClass( s ):" ), SubClass.isBaseClass( s ) ); console.log( chalk.cyan( "SubClass.isSubClass( s ):" ), SubClass.isSubClass( s ) );