Scope of var and variables

If I have a function like

<cfscript>
function say(what) {
  var a = what;
  variables.b = what;
  return what;
}
</cfscript>

I thought that the scope of a was variables, but dumping variablesreturns only b. What is the scope?

+4
source share
5 answers

Declaring a variable using a keyword varputs it in a region local, not in a region variables.

+2
source

This is indeed a comment, but it is too long. Consider the following code

<cfscript>
function doMath() {
   var a = 1;
   local.b = 2;
   return a + local.b;
   }
</cfscript>

At first glance it may seem that varthey local.have the same scope. After all, they both exist only within the function. When a function is executed, both variables cease to exist. The end of the story? Probably no.

ColdFusion , .

url.a
form.a
cookie.a
session.a
application.a
local.a
arguments.a
myQuery.a

. , <cfoutput>#a#</cfoutput>, a ? ColdFusion , , . , . .

, , var, ColdFusion, , , . local.a, .

, . , , . , .

. var local.

+3

(. ):

<cfscript>
function say(what) {

    // using the "var" keyword define a variable named "a" in the "local" scope
    // the "local" scope is private to the current function context
    // give to "a" the value of the "what" argument
  var a = what;


    // explicitly define a variable "b" in the "variables" scope 
    // the "variables" scope is available anywhere in the current template context
    // give to "b" the value of the "what" argument
  variables.b = what;


    // return the value of "what" argument
  return what;
}
</cfscript>

var -
variables -
local - ( )

local var a = "foo", local.a = "foo".

+2

, . var a a local, local. , a, local.

https://trycf.com/gist/faf04daa53194a5fad2e69e164518299/acf2016?theme=monokai

<cfscript>
function say() {
    local.a = "local" ; 
    var b   = "var" ;

    lv = local.b ; // We didn't explicitly assign b to Local scope. 

    try {
        v = variables ; // Let dump the variables scope. 
    } catch (any e) {
        v = "Error: " & e.message ;
    }

    variables.nh = "Now here." ; // Explicitly populate variables scope.

    var c = "var c" ; // We have a variables scope, what happens to var?

    try {
        v2 = variables ; // Let dump the variables scope. 
    } catch (any e) {
        v2 = "Error: " & e.message ;
    }

    var d = "var" ;
    local.d = "local" ;

    local.e = "local" ;
    var e = "var" ;

    return {
        a  : a ,       // Local.
        b  : b ,       // Var.
        d  : d ,       // Which one?
        e  : e ,       // Which one?
        el : local.e , // Which one?? 
        l  : lv ,      // Ref b in local scope.
        l2 : local ,   // Dump local scope.
        v  : v ,       // There doesn't seem to be a variables scope yet.
        v2 : v2        // Defined a variable scope and now here.
    } ;
}

writeDump(say());
</cfscript>

Output 1

, var b b local variables , - . local.b, variables.b . variables nh var c. variables, local.

local, var, (. d, e el). .

, arguments local.

, :

https://trycf.com/gist/65b73e7a57d0434049d0eb9c0d5f9687/acf11?theme=monokai

<cfscript>
function ks() {
    variables.jay   = "Snoogins." ; // variables scope leaks out of function.
    local.silentbob = "____" ;      // local scope stays inside function.
}

function sayArgs(arg) {
    local.arg = "local" ; // Order after agruments in CF10 but not 2016.

    ks() ; // Run ks() function to instantiate jay and silentbob.

    return {
        arg    : arg ,       // CF10 = arguments scope. CF11+ = local scope.
        args   : arguments ,
        local  : local ,     // No leakage from ks().
        vars   : variables   // Jay leaks from ks().
    } ;
}

writeDump(sayArgs("argue")) ;
</cfscript>

Output 2

, :

-, arguments local CF10 CF. CF2016 ( 2011+) , local arguments, . CF10 : arguments. Lucee Railo ACF2016.

, variables local. local , . variables .

https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/using-coldfusion-variables/about-scopes.html < < ACF2016. local arguments.

http://www.learncfinaweek.com/week1/Scopes/ < < , ACF10 , arguments local .

https://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09af4-7fdf.html < < ACF9. , ACF2016, ACF10. CF9 , , CF9 .

+2

In the current example, var ais only available within the scope.

0
source

Source: https://habr.com/ru/post/1688733/


All Articles