Content deleted Content added
Noahfgodard (talk | contribs) added wikilinks and removed "underlinked" tag |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 7:
Unless it's within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:
<
<cfscript>
xParam = 115;
Line 13:
color = 'FFCC99';
</cfscript>
</syntaxhighlight>
A simple example of a [[Subroutine|function]]:
<
<cfscript>
function Sum(a, b) {
Line 23:
}
</cfscript>
</syntaxhighlight>
A simple example of a component in CFScript, containing two functions:
<
component {
public void function foo() {
Line 37:
}
}
</syntaxhighlight>
ColdFusion 11, [[Railo]] 4.1+, and [[Lucee]] 4.5+ both do their best to fully support cf tags in CFScript.
While there may not be direct substitutions for all tags, it is often still possible to achieve the results of a tag in script, but via a different syntax. For example, this is how to get a query into a variable in CFSCRIPT without writing a [[User-defined function|UDF]]:
<
<cfscript>
qGetData = new Query();
Line 48:
qDateResult = qGetData .Execute().getResult();
</cfscript>
</syntaxhighlight>
== Syntax ==
Line 84:
=== Comments ===
CFScript has two forms of comments: single line and multiline.
<
// This is a single-line comment.
// This is a second single-line comment.
</syntaxhighlight>
<
/* This is a multiline comment.
You do not need to start each line with a comment indicator.
This line is the last line in the comment. */
</syntaxhighlight>
=== Try / Catch ===
<
try {
throw(message="Oops", detail="xyz");
Line 105:
WriteOutput("I run even if no error");
}
</syntaxhighlight>
=== Switch statement ===
<
switch (car) {
case "Nissan":
Line 119:
WriteOutput("I'm exotic");
}
</syntaxhighlight>
=== Looping ===
==== For Loop ====
<
for (i=1; i LTE ArrayLen(array); i=i+1) {
WriteOutput(array[i]);
}
</syntaxhighlight>
==== FOR IN Loop ====
<
struct = StructNew();
struct.one = "1";
Line 139:
}
//OUTPUTS onetwo
</syntaxhighlight>
==== While Loop ====
<
x = 0;
while (x LT 5) {
Line 149:
}
// Outputs: 12345
</syntaxhighlight>
==== Do / While Loop ====
<
x = 0;
do {
Line 159:
} while (x LTE 0);
// Outputs: 1
</syntaxhighlight>
==== Looping over an Array ====
<
for (item in array) {
doSomething(item);
}
</syntaxhighlight>
== Differences from JavaScript ==
|