我正在玩 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"
谢谢!