Syntax for defining a component is as follows:
/**
* ColdFusion treats plain comment text as a hint.
* You can also use the @hint metadata name for hints.
* Set metadata, including, optionally, attributes, (including custom
* attributes) in the last entries in the comment block, as follows:
*@metadataName metadataValue
...
*/
component attributeName="attributeValue" ... {
body contents
}
The following example shows a simple component definition
/**
* Simple Component.
*/
component {
/**
* Simple function.
*/
public void function foo() {
WriteOutput("Method foo() called<br>");
}
}
When you define a component entirely in CFScript, you do not
have to use a cfscript tag on the page. In this
case, the component keyword can be preceded only by comments (including
metadata assignments) and import operators. Adobe recommends this
format as a best practice. You specify component properties as follows:
/**
/*@default defaultValue
* @attrib1Name attrib1Value
* ...
*/
property [type]propName;
If the type precedes the property name, you do not need to use
the "type" keyword, only the name of the specific type. In either
format, you must set the name attribute value. All other property
attributes, such as type, are optional. As with cfproperty tags,
place the property operators at the top of the component definition,
immediately following the opening brace.
The syntax to define a function is similar to the component definition:
/**
*Comment text, treated as a hint.
*Set metadata, including, optionally, attributes, in the last entries
*in the comment block, as follows:
*@metadataName metadataValue
...
*/
access returnType function functionName(arg1Type arg1Name="defaultValue1"
arg1Attribute="attributeValue...,arg2Type
arg2Name="defaultValue2" arg2Attribute="attributeValue...,...)
functionAttributeName="attributeValue" ... {
body contents
}
You specify all function arguments, including the argument type,
default value, and attributes in the function definition.
The following example shows a function definition:
/**
* @hint "This function displays its name and parameter."
*/
public void function foo(String n1=10)
description="does nothing" hint="overrides hint" (
WriteOutput("Method foo() called<br> Parameter value is " & n1);
}
Specifying the required keyword makes the argument
mandatory. If the required keyword is not present
then the argument becomes optional.
For example:
public function funcname(required string argument1)
Interface definitions follow the same pattern as components,
with the same general rules and limitations that apply to the interfaces
you define using cfinterface tags. The following simple code defines
an interface with a single function that takes one string argument,
with a default argument value of "Hello World!":
interface {
function method1(string arg1="Hello World!");
function method2 (string arg1="Goodbye World!");
...
}
The following example shows the definition of a simple component
with a single function:
/**
* Component defined in CFScript
* @output true
*/
component extends="component_01" {
/**
* Function that displays its arguments and returns a string.
* @returnType string
*/
public function method_03(argOne,argTwo) {
WriteOutput("#arguments.argOne# ");
WriteOutput("#arguments.argTwo# ");
return("Arguments written.");
}
}