可以将一个 <cfcomponent> 当做一个 class, 然后将 <cffunction> 当成一个方法

cffunction

格式

<cffunction 
    name = "method name" 
    access = "method access" 
    description = "function description" 
    displayName = "name" 
    hint = "hint text" 
    output = "yes|no" 
    returnFormat = "not specified|JSON|plain|WDDX" 
    returnType = "data type" 
    roles = "securityRoles" 
    secureJSON = "yes|no" 
    verifyClient = "no|yes">

cfcomponent

用于 CFC 声明

示例

<cfcomponent>
  <!--- 这里是一个函数 funcwitharg,要求输入一个不可忽略的名为 arg1 的参数 --->
  <cffunction name="funcwitharg" returntype="Numeric">
    <cfargument name="arg1" type="Numeric" required="yes">
      <cfset arg1=#arg1#+1>
        <cfreturn arg1>
  </cffunction>

  <!--- 这里是一个函数 funcwithoutarg,不需要输入参数直接返回 10 --->
  <cffunction name="funcwithoutarg" returntype="Numeric">
    <cffunctionfreturn 10>
  </cffunction>
</cfcomponent>

参数说明

  • name

对应的函数的名称

  • returntype
  • output

可选, 函数也可以输出数据。 但是不常用, 设置成 False 还可以减少调用时出现的空白

cfargument

使用 <cfargument> 创建参数

参数说明

  • name
  • type
  • required

表示这个参数是不是不可忽略的

  • default

默认值, 如果没有传进参数的话会使用这个值

函数调用

调用函数有以下方法:

  1. 调用组建的方法

  2. 直接使用 cfinvoke 配合 cfinvokeargument 调用

  3. 调用实例的方法

  4. 使用 cfobject 创建一个实例然后将实例用作 cfinvokecomponent 属性, 然后配合 cfinvokeargument 调用(似乎是很多余的操作……)

  5. 使用 cfscript 实现

    1. 使用 cfobject 创建一个实例,然后通过实例调用
    2. 使用 cfobject 创建示例的过程也可以使用 createObject()方法来替代

cfinvoke

对函数进行调用, 可以使用参数 注意在同一个文件里使用 <cffunction> 的话可以省去>的话可以省去外围的 <cfcomponent> 标签

<cfinvoke component="function" method="funcwitharg" returnvariable="return1">
    <cfinvokeargument name="arg1" value="3">
    </cfinvokeargument>
</cfinvoke>

参数说明

  • component

决定需要调用的函数所在的文件, component="function" 表示对应的函数写在 function.cfc 之中 实际上这里指的是路径

  • method

需要调用的函数的 name 的值

  • returnvariable

接收返回值的变量名称, 如果对应变量未定义则会自动新建

动态参数 Dynamic Arguments

很多时候调用函数一些参数不需要传递因为有默认值, 因此可以用以下方法赋值

#ck.getMetricTable(
    query = qInspectionCompletion,
    selectedOrg = bigquery.orgname,
    selectedSuborg = bigquery.suborg, 
    selectedSite = bigquery.location
)

几个要点:

  • 可以不用提供所有的值
  • 似乎在函数定义的时候不能设定对应的参数类型, 否则没有提供的参数会报类型检查错误(传过去的是 null 而不是特定值的错误)
  • 不需要按照参数的顺序提供参数

函数返回值 cfreturn

直接写变量名就可以了, 不需要井号: <cfreturn varname>

cfscript

和 JAVA 中的用法类似 测试时发现需要将所有的参数按顺序传入

示例

    <cfobject name="obj" component="function"/>
    <cfset result2=obj.funcwitharg(4)/>
    <cfoutput>
        #result2#
    </cfoutput>
    <cfset result3=obj.funcwitharg(4,5)/>
    <cfoutput>
        #result3#
    </cfoutput>

function.cfc

<cfcomponent>
  <!--- 这里是一个函数 funcwitharg,要求输入一个不可忽略的名为 arg1 的参数 --->
  <cffunction name="funcwitharg" returntype="Numeric">
    <cfargument name="arg1" type="Numeric" required="yes">
      <cfargument name="arg2" type="Numeric" required="no">

        <cfif isDefined("arg2")>
          <cfset arg1=#arg1#+#arg2#>
            <cfelse>
              <cfset arg1=#arg1#+1>
        </cfif>
        <cfreturn arg1>
  </cffunction>
</cfcomponent>

cfObject

示例

<cfset object=createObject("component","function") />
<cfoutput>
  #object.funcwitharg(1,2,3)#
  #object.funcwitharg(arg1="1",arg3="5")#
</cfoutput>