CFScript: Difference between revisions

From HandWiki
imported>ScienceGen
correction
 
MainAI6 (talk | contribs)
link
 
Line 2: Line 2:


== Usage ==
== Usage ==
Unless it's within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:
Unless it is within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:
<source lang="cfm">
<syntaxhighlight lang="cfm">
<cfscript>
<cfscript>
xParam = 115;
xParam = 115;
Line 9: Line 9:
color = 'FFCC99';
color = 'FFCC99';
</cfscript>
</cfscript>
</source>
</syntaxhighlight>


A simple example of a [[Subroutine|function]]:
A simple example of a [[Subroutine|function]]:
<source lang="cfm">
<syntaxhighlight lang="cfm">
<cfscript>
<cfscript>
function Sum(a, b) {
function Sum(a, b) {
Line 19: Line 19:
}
}
</cfscript>
</cfscript>
</source>
</syntaxhighlight>


A simple example of a component in CFScript, containing two functions:
A simple example of a component in CFScript, containing two functions:
<source lang="javascript">
<syntaxhighlight lang="javascript">
component {
component {
     public void function foo() {
     public void function foo() {
Line 33: Line 33:
     }
     }
}
}
</source>
</syntaxhighlight>


ColdFusion 11, [[Software:Railo|Railo]] 4.1+, and [[Lucee]] 4.5+ both do their best to fully support cf tags in CFScript.
ColdFusion 11, [[Software:Railo|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]]:
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]]:
<source lang="cfm">
<syntaxhighlight lang="cfm">
<cfscript>
<cfscript>
qGetData = new Query();
qGetData = new Query();
Line 44: Line 44:
qDateResult = qGetData.Execute().getResult();
qDateResult = qGetData.Execute().getResult();
</cfscript>
</cfscript>
</source>
</syntaxhighlight>


== Syntax ==
== Syntax ==
Line 80: Line 80:
=== Comments ===
=== Comments ===
CFScript has two forms of comments: single line and multiline.
CFScript has two forms of comments: single line and multiline.
<source lang="javascript">
<syntaxhighlight lang="javascript">
// This is a single-line comment.
// This is a single-line comment.
// This is a second single-line comment.
// This is a second single-line comment.
</source>
</syntaxhighlight>


<source lang="javascript">
<syntaxhighlight lang="javascript">
/* This is a multiline comment.
/* This is a multiline comment.
   You do not need to start each line with a comment indicator.
   You do not need to start each line with a comment indicator.
   This line is the last line in the comment. */
   This line is the last line in the comment. */
</source>
</syntaxhighlight>


=== Try / catch ===
=== Try / catch ===
<source lang="javascript">
<syntaxhighlight lang="javascript">
try {
try {
     throw(message="Oops", detail="xyz");
     throw(message="Oops", detail="xyz");
Line 101: Line 101:
     WriteOutput("I run even if no error");
     WriteOutput("I run even if no error");
}
}
</source>
</syntaxhighlight>


=== Switch statement ===
=== Switch statement ===
<source lang="javascript">
<syntaxhighlight lang="javascript">
switch (car) {
switch (car) {
     case "Nissan":
     case "Nissan":
Line 115: Line 115:
         WriteOutput("I'm exotic");
         WriteOutput("I'm exotic");
}
}
</source>
</syntaxhighlight>


=== Looping ===
=== Looping ===


==== For loop ====
==== For loop ====
<source lang="javascript">
<syntaxhighlight lang="javascript">
for (i=1; i <= ArrayLen(array); i=i+1) {
for (i=1; i <= ArrayLen(array); i=i+1) {
     WriteOutput(array[i]);
     WriteOutput(array[i]);
}
}
</source>
</syntaxhighlight>


==== FOR IN Loop ====
==== FOR IN Loop ====
<source lang="javascript">
<syntaxhighlight lang="javascript">
struct = StructNew();
struct = StructNew();
struct.one = "1";
struct.one = "1";
Line 135: Line 135:
}
}
//OUTPUTS onetwo
//OUTPUTS onetwo
</source>
</syntaxhighlight>


==== While loop ====
==== While loop ====
<source lang="javascript">
<syntaxhighlight lang="javascript">
x = 0;
x = 0;
while (x < 5) {
while (x < 5) {
Line 145: Line 145:
}
}
// Outputs: 12345
// Outputs: 12345
</source>
</syntaxhighlight>


==== Do / while loop ====
==== Do / while loop ====
<source lang="javascript">
<syntaxhighlight lang="javascript">
x = 0;
x = 0;
do {
do {
Line 155: Line 155:
} while (x <= 0);
} while (x <= 0);
// Outputs: 1
// Outputs: 1
</source>
</syntaxhighlight>


==== Looping over an array ====
==== Looping over an array ====
<source lang="javascript">
<syntaxhighlight lang="javascript">
for (item in array) {
for (item in array) {
     doSomething(item);
     doSomething(item);
}
}
</source>
</syntaxhighlight>


== Differences from JavaScript ==
== Differences from JavaScript ==

Latest revision as of 11:59, 14 February 2026

CFScript is an extension of CFML on the ColdFusion platform. CFScript resembles JavaScript. Some ColdFusion developers prefer it since it has less visual and typographical overhead than ordinary CFML.[clarification needed]

Usage

Unless it is within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:

<cfscript>
xParam = 115;
yParam = 200;
color = 'FFCC99';
</cfscript>

A simple example of a function:

<cfscript>
function Sum(a, b) {
    var sum = a + b;
    return sum;
}
</cfscript>

A simple example of a component in CFScript, containing two functions:

component {
    public void function foo() {
        WriteOutput("Method foo() called<br/>");
    }

    public function getString() {
        var x = "hello";
        return x;
    }
}

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 UDF:

<cfscript>
qGetData = new Query();
qGetData.setDataSource('#Application.datasource#');
qGetData.setSQL('SELECT column1, column2 FROM table WHERE 1');
qDateResult = qGetData.Execute().getResult();
</cfscript>

Syntax

Since ColdFusion 8, CFScript has supported syntax abbreviations that are common in many other programming languages, such as "++", "<=" and "+=".[1]

Arithmetic operators

Operator Description
+ - * / Basic arithmetic: Addition, subtraction, multiplication, and division.

In division, the right operand cannot be zero.

++ -- Increment and decrement. Increase or decrease the variable by one.

These operators can be used for pre-incrementing or decrementing (as in x = ++ i), where the variable is changed before it is used in the expression. They can also be used for post-incrementing or decrementing (as in x = i++), where the value is changed after it is used in the expression. If the value of the variable i is initially 7, for example, the value of x in x = ++i is 8 after expression evaluation, but in x=i++, the value of x is 7. In both cases, the value of i becomes 8.

These operators cannot be used with expressions that involve functions, as in f().a++. Also, you can use an expression such as -++x, but ---x and +++x cause errors, because their meanings are ambiguous. You can use parentheses to group the operators, as in -(--x) or +(++x), however.

+= -= *= /= %= Compound assignment operators. The variable on the right is used as both an element in the expression and the result variable. Thus, the expression a += b is equivalent to a = a +b.

An expression can have only one compound assignment operator.

+ - Unary arithmetic: Set the sign of a number.
MOD or % Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3).
\ Integer division: Divide an integer by another integer. The result is also an integer; for example, 9\4 is 2. The right operand cannot be zero
^ Exponentiation: Return the result of a number raised to a power (exponent). Use the caret character (^) to separate the number from the power; for example, 2^3 is 8. Real and negative numbers are allowed for both the base and the exponent. However, any expression that equates to an imaginary number, such -1^.5 results in the string "-1.#IND. ColdFusion does not support imaginary or complex numbers.

Comments

CFScript has two forms of comments: single line and multiline.

// This is a single-line comment.
// This is a second single-line comment.
/* 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. */

Try / catch

try {
    throw(message="Oops", detail="xyz");
} catch (any e) {
    WriteOutput("Error: " & e.message);
    rethrow;
} finally {
    WriteOutput("I run even if no error");
}

Switch statement

switch (car) {
    case "Nissan":
         WriteOutput("I own a Nissan");
         break;
    case "Toyota":
         WriteOutput("I own a Toyota");
         break;
    default:
         WriteOutput("I'm exotic");
}

Looping

For loop

for (i=1; i <= ArrayLen(array); i=i+1) {
    WriteOutput(array[i]);
}

FOR IN Loop

struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
    WriteOutput(key);
}
//OUTPUTS onetwo

While loop

x = 0;
while (x < 5) {
    x = x + 1;
    WriteOutput(x);
}
// Outputs: 12345

Do / while loop

x = 0;
do {
    x = x + 1;
    WriteOutput(x);
} while (x <= 0);
// Outputs: 1

Looping over an array

for (item in array) {
    doSomething(item);
}

Differences from JavaScript

Although CFScript and JavaScript are similar, they have several key differences. The following list identifies CFScript features that differ from JavaScript:

  • CFScript uses ColdFusion expression, which are not a superset or a subset of JavaScript expressions. In particular, ColdFusion expressions do not support bitwise operators, and the ColdFusion MOD or % operator operates differently from the corresponding JavaScript % operator: In ColdFusion, the operator does integer arithmetic and ignores fractional parts. ColdFusion expressions also support the EQV, IMP, CONTAINS, and DOES NOT CONTAIN operators that are not supported in JavaScript.
  • Variable declarations (var keyword) are only used in user-defined functions and threads.
  • CFScript is not case sensitive.
  • All statements end with a semicolon, and line breaks in the code are ignored.
  • Assignments are statements, not expressions, and therefore cannot be used in situations that require evaluating the assignment operation.
  • JavaScript objects, such as window and document, are not available.
  • Only the ColdFusion server processes CFScript. There is no client-side CFScript.

References