Jump to content

CFScript

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Doggum (talk | contribs) at 12:12, 14 August 2013. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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; 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.

Usage

All CFScript code must be contained within a CFScript tag pair, as follows:

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

A Simple function example.

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

Simple component example in cfscript, containing two function

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

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

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]

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, 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