Mozier's weblog

<cfweblog author="Mozier" attention="ColdFusion,BlueDragon,CFML,Model-Glue,Mach-ii,etc" />
  订阅 订阅  :: 管理

Creating ColdFusion's missing "instanceof" method

Posted on 2006-04-08 10:51  Mozier  阅读(1279)  评论(0编辑  收藏  举报
在ColdFusion中没有instanceof关健字,所以只能自己写个函数代替,用来比较一个对象是否是另
一个类的实例:
 1 <cffunction name="isInstanceOf" access="public" returntype="Boolean" output="false">
 2   <cfargument name="obj" type="any" required="true"/>
 3   <cfargument name="reqType" type="string" required="true" />
 4   <cfset var searchMd = getMetaData(obj) />
 5   <cfif searchMd.name IS reqType >
 6     <cfreturn true />
 7   <cfelse> 
 8     <cfloop condition="#StructKeyExists(searchMd, "extends")#">
 9        <cfset searchMd = searchMd.extends />
10        <cfif searchMd.name IS reqType>
11          <cfreturn true />
12        </cfif>
13     </cfloop>
14   </cfif>
15   <cfreturn false />
16 </cffunction>
17 
同时可以参考Hal HelmsBaseComponent.cfc中的isInstanceOf函数.