From c9d0e027faca3fef715fb382bd3d22701aa8391d Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 28 Dec 2010 20:58:42 -0500 Subject: [PATCH] Added isClass() and isClassInstance() methods --- lib/class.js | 37 +++++++++++++++++++++++++++++++++++++ test/test-class.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/lib/class.js b/lib/class.js index a307f13..d79c86b 100644 --- a/lib/class.js +++ b/lib/class.js @@ -39,6 +39,43 @@ exports.extend = function( base ) } +/** + * Determines whether the provided object is a class created through ease.js + * + * @param {Object} obj object to test + * + * @return {boolean} true if class (created through ease.js), otherwise false + */ +exports.isClass = function( obj ) +{ + obj = obj || {}; + + return ( obj.prototype instanceof Class ) + ? true + : false + ; +}; + + +/** + * Determines whether the provided object is an instance of a class created + * through ease.js + * + * @param {Object} obj object to test + * + * @return {boolean} true if instance of class (created through ease.js), + * otherwise false + */ +exports.isClassInstance = function( obj ) +{ + obj = obj || {}; + + return ( obj instanceof Class ) + ? true + : false; +}; + + /** * Default class implementation * diff --git a/test/test-class.js b/test/test-class.js index 5435dcc..c7ec50a 100644 --- a/test/test-class.js +++ b/test/test-class.js @@ -44,6 +44,43 @@ assert.ok( "Extend method creates a new object" ); +assert.ok( + ( Class.isClass( Class.extend() ) ), + "Classes are considered by the system to be classes" +); + + +// +// isClass +assert.ok( + ( !( Class.isClass( {} ) ) ), + "Only actual classes are considered to be classes" +); + +assert.ok( + ( !( Class.isClass( new Foo() ) ) ), + "Class instances are not considered to be classes (they are objects)" +); + + +// +// isClassInstance +assert.ok( + ( Class.isClassInstance( new Foo() ) ), + "Class instances are considered to be classes instances" +); + +assert.ok( + ( !( Class.isClassInstance( Foo ) ) ), + "Classes are not considered to be class instances" +); + +assert.ok( + ( !( Class.isClassInstance( {} ) ) ), + "Other objects are not considered to be class instances" +); + + // only perform check if supported by the engine if ( Object.isFrozen ) {