0

我正在玩 JS.Class v.3.0 http://jsclass.jcoglan.com/,我想找到一种方法来检测对象的实例。

            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?

如您所见,该getColors方法可以接受两个参数,它们将返回整个哈希、包含 1 个值的选定哈希或仅返回选定值。

问题是:我如何检查是否F150Colors是 的实例JS.Hash

解决方案:typeof F150Colors == "string"还不够,因为最终,我的hColors遗嘱包含对象 ( {})。一切都将是typeof F150Colors == "object"

谢谢!

4

1 回答 1

4
if (F150Colors instanceof JS.Hash)
于 2011-06-14T15:01:40.350 回答