CFScript: Difference between revisions

Content deleted Content added
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:
<sourcesyntaxhighlight lang="cfm">
<cfscript>
xParam = 115;
Line 13:
color = 'FFCC99';
</cfscript>
</syntaxhighlight>
</source>
 
A simple example of a [[Subroutine|function]]:
<sourcesyntaxhighlight lang="cfm">
<cfscript>
function Sum(a, b) {
Line 23:
}
</cfscript>
</syntaxhighlight>
</source>
 
A simple example of a component in CFScript, containing two functions:
<sourcesyntaxhighlight lang="javascript">
component {
public void function foo() {
Line 37:
}
}
</syntaxhighlight>
</source>
 
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]]:
<sourcesyntaxhighlight lang="cfm">
<cfscript>
qGetData = new Query();
Line 48:
qDateResult = qGetData .Execute().getResult();
</cfscript>
</syntaxhighlight>
</source>
 
== Syntax ==
Line 84:
=== Comments ===
CFScript has two forms of comments: single line and multiline.
<sourcesyntaxhighlight lang="javascript">
// This is a single-line comment.
// This is a second single-line comment.
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="javascript">
/* 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>
</source>
 
=== Try / Catch ===
<sourcesyntaxhighlight lang="javascript">
try {
throw(message="Oops", detail="xyz");
Line 105:
WriteOutput("I run even if no error");
}
</syntaxhighlight>
</source>
 
=== Switch statement ===
<sourcesyntaxhighlight lang="javascript">
switch (car) {
case "Nissan":
Line 119:
WriteOutput("I'm exotic");
}
</syntaxhighlight>
</source>
 
=== Looping ===
 
==== For Loop ====
<sourcesyntaxhighlight lang="javascript">
for (i=1; i LTE ArrayLen(array); i=i+1) {
WriteOutput(array[i]);
}
</syntaxhighlight>
</source>
 
==== FOR IN Loop ====
<sourcesyntaxhighlight lang="javascript">
struct = StructNew();
struct.one = "1";
Line 139:
}
//OUTPUTS onetwo
</syntaxhighlight>
</source>
 
==== While Loop ====
<sourcesyntaxhighlight lang="javascript">
x = 0;
while (x LT 5) {
Line 149:
}
// Outputs: 12345
</syntaxhighlight>
</source>
 
==== Do / While Loop ====
<sourcesyntaxhighlight lang="javascript">
x = 0;
do {
Line 159:
} while (x LTE 0);
// Outputs: 1
</syntaxhighlight>
</source>
 
==== Looping over an Array ====
<sourcesyntaxhighlight lang="javascript">
for (item in array) {
doSomething(item);
}
</syntaxhighlight>
</source>
 
== Differences from JavaScript ==