CFScript: Difference between revisions

Content deleted Content added
Usage: clean up, typo(s) fixed: For example → For example, using AWB
m rm contraction
Tags: Mobile edit Mobile app edit iOS app edit App section source
 
(10 intermediate revisions by 9 users not shown)
Line 1:
{{refimproveMore citations needed|date =November 2006}}
{{multiple issues|
{{Underlinked|date=January 2013}}
{{refimprove|date =November 2006}}
}}
 
'''CFScript''' is an extension of [[ColdFusion Markup Language|CFML]] on the ColdFusion platform. CFScript resembles [[JavaScript]]. Some ColdFusion [[Software Developers|developers]] prefer it since it has less visual and typographical overhead than ordinary CFML.{{Clarify|reason=|date=March 2020}}
 
== Usage ==
Unless it is within a pure script-based ColdFusion Component, all CFScript code must be contained within a CFScript tag pair as follows:
It is considered best practice to write ColdFusion Components and all business logic in CFScript and to use [[CFML]] only in .cfm files amongst HTML.
<sourcesyntaxhighlight lang="cfm">
 
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">
<cfscript>
xParam = 115;
yParam = 200;
color = 'FFCC99';
</cfscript>
</syntaxhighlight>
</source>
 
A simple example of a [[Subroutine|function]]:
<sourcesyntaxhighlight lang="cfm">
<cfscript>
function Sum(a, b) {
Line 26 ⟶ 21:
}
</cfscript>
</syntaxhighlight>
</source>
 
A simple example of a component in CFScript, containing two functions:
<sourcesyntaxhighlight lang="javascript">
component {
public void function foo() {
Line 40 ⟶ 35:
}
}
</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();
qGetData .setDataSource('#APPLICATIONApplication.datasource#');
qGetData .setSQL('SELECT column1, column2 FROM table WHERE 1');
qDateResult = qGetData .Execute().getResult();
</cfscript>
</syntaxhighlight>
</source>
 
== Syntax ==
 
Since ColdFusion 8, CFScript has supported [[Syntax (programming languages)|syntax]] abbreviations that are common in many other programming languages, such as "++", "<=" and "+=".<ref>Nadel,{{Cite Ben. httpweb|url=https://www.bennadel.com/blog/741-Learninglearning-ColdFusioncoldfusion-8-Allall-Hailhail-Thethe-Newnew-Operatoroperator.htm|title=Learning ColdFusion 8: All Hail The New ++ Operator|last=Nadel|first=Ben|date=May 31, 2007|website=|archive-url=|archive-date=|access-date=}}</ref>
 
=== Arithmetic operators ===
Line 87 ⟶ 82:
=== 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 / Catchcatch ===
<sourcesyntaxhighlight lang="javascript">
try {
throw(message="Oops", detail="xyz");
Line 108 ⟶ 103:
WriteOutput("I run even if no error");
}
</syntaxhighlight>
</source>
 
=== Switch statement ===
<sourcesyntaxhighlight lang="javascript">
switch (car) {
case "Nissan":
Line 122 ⟶ 117:
WriteOutput("I'm exotic");
}
</syntaxhighlight>
</source>
 
=== Looping ===
 
==== For Looploop ====
<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 142 ⟶ 137:
}
//OUTPUTS onetwo
</syntaxhighlight>
</source>
 
==== While Looploop ====
<sourcesyntaxhighlight lang="javascript">
x = 0;
while (x LT< 5) {
x = x + 1;
WriteOutput(x);
}
// Outputs: 12345
</syntaxhighlight>
</source>
 
==== Do / Whilewhile Looploop ====
<sourcesyntaxhighlight lang="javascript">
x = 0;
do {
x = x + 1;
WriteOutput(x);
} while (x LTE<= 0);
// Outputs: 1
</syntaxhighlight>
</source>
 
==== Looping over an Arrayarray ====
<sourcesyntaxhighlight lang="javascript">
for (item in array) {
doSomething(item);
}
</syntaxhighlight>
</source>
 
== 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 expressions[[Expression (computer science)|expression]], which are not a superset or a subset of JavaScript expressions. In particular, ColdFusion expressions do not support [[Bitwise operation|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 declaration|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.
Line 187 ⟶ 182:
== External links ==
* [http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7feb.html Extending ColdFusion Pages with CFML Scripting - Adobe]
* [https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/extending-coldfusion-pages-with-cfml-scripting.html]
 
{{DEFAULTSORT:Cfscript}}