0

I am playing with JS.Class v.3.0 http://jsclass.jcoglan.com/ and I would like to find a way to detect the instance of an object.

            var Car = new JS.Class({
                hColors : new JS.Hash([]),

                initialize : function() {
                    this.hColors.store( "1", "red " ),
                    this.hColors.store( "2", "green " ),
                    this.hColors.store( "3", "blue " )
                },

                getColors : function( colorId, returnHash ) {
                    if ( this.hColors.get( colorId ) )
                    {
                        var currentObjects = this.hColors.get( colorId );

                        if ( returnHash )
                        {
                            return new JS.Hash([
                                colorId, currentObjects 
                            ]);
                        }

                        return currentObjects;
                    }

                    return this.hColors;
                }
            });

            var F150 = new Car();

            var F150Colors = F150.getColors(); //Hash:{1=>red ,2=>green ,3=>blue }
            //var F150Colors = F150.getColors( "2", false ); //String:green
            //var F150Colors = F150.getColors( "2", true ); //Hash:{2=>green }

            //How do I test if F1250 is an instance of JS.Hash?

As you can see, the getColors method can accept two arguments which will either return the whole Hash, a selected hash containing 1 value or simply the selected value.

The question is: How could I check if F150Colors is an instance of JS.Hash ?

The solution: typeof F150Colors == "string" is not enough because eventually, my hColors will contains objects ( {} ). And everything will be a typeof F150Colors == "object"

Thank you!

Cybrix
  • 3,248
  • 5
  • 42
  • 61
  • 1
    As reko_t answered below, you have you `instanceof`. `typeof` only yields values that correspond to the build-in types in JavaScript, as you found out. – Peter Jun 14 '11 at 15:04

1 Answers1

4
if (F150Colors instanceof JS.Hash)
reko_t
  • 55,302
  • 10
  • 87
  • 77