CFScript: Difference between revisions

Content deleted Content added
Doggum (talk | contribs)
m <source>
Line 8:
 
All CFScript code must be contained within a CFScript tag pair as follows, unless it's within a pure script based Coldfusion Component.
<source lang="cfm">
<pre>
<cfscript>
xParam = 115;
Line 14:
color = 'FFCC99';
</cfscript>
</presource>
 
A Simple function example.
<source lang="cfm">
<pre>
<cfscript>
function Sum(a,b) {
Line 24:
}
</cfscript>
</presource>
 
Simple component example in cfscript, containing two function
<source lang="javascript">
<pre>
component {
public void function foo(){
WriteOutput("Method foo() called<nowiki><br></nowiki>");
}
 
Line 38:
}
}
</presource>
 
Coldfusion 11 and Railo 4.1 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 UDF:
<source lang="cfm">
<pre>
<cfscript>
qGetData = new Query();
Line 48:
qGetData .setSQL('SELECT column1, column2 FROM table WHERE 1');
qDateResult = qGetData .Execute().getResult();
</cfscript></presource>
 
==Syntax==
Line 83:
=== Comments ===
CFScript has two forms of comments: single line and multiline.
<source lang="javascript">
<pre>
//This is a single-line comment.
//This is a second single-line comment.
</presource>
 
<source lang="javascript">
<pre>
/* 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. */
</presource>
=== Try / Catch ===
<source lang="javascript">
<pre>
try {
throw(message="Oops", detail="xyz");
Line 103:
WriteOutput("I run even if no error");
}
</presource>
=== Switch statement ===
<source lang="cfm">
<pre>
switch(car) {
case "Nissan":
Line 116:
WriteOutput("I'm exotic");
}
</presource>
 
=== Looping ===
==== For Loop ====
<source lang="javascript">
<pre>
for (i=1;i LTE ArrayLen(array);i=i+1) {
WriteOutput(array[i]);
}
</presource>
 
==== FOR IN Loop ====
<source lang="javascript">
<pre>
struct = StructNew();
struct.one = "1";
Line 135:
}
//OUTPUTS onetwo
</presource>
 
==== While Loop ====
<source lang="javascript">
<pre>
x = 0;
while (x LT 5) {
Line 145:
}
//OUTPUTS 12345
</presource>
 
==== Do / While Loop ====
<source lang="javascript">
<pre>
x = 0;
do {
Line 155:
} while (x LTE 0);
// OUTPUTS 1
</presource>
 
==== Looping over an Array ====
<source lang="javascript">
<pre>
for(item in array) {
doSomething(item);
}
</presource>
 
== Differences from JavaScript ==