如何在 CF10 中确定闭包变量的范围? [英] How to scope closure's variables in CF10?

查看:17
本文介绍了如何在 CF10 中确定闭包变量的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引自 Adobe ColdFusion 10:使用闭包文档:

<块引用>

function helloTranslator(String helloWord){返回函数(字符串名称){返回#helloWord#,#name#";};}

如何在返回行上正确地确定 helloWordname 的范围?它们都在 Arguments 范围内吗?如果是这样,它们一定是唯一的吗?

闭包和函数部分还提到在已经很长的范围搜索之上还有 4 个范围:

<块引用>

最后,以下是搜索无作用域变量的顺序:

  1. 闭包的本地作用域
  2. 闭包的参数范围
  3. 外部函数的 local 范围(如果可用)
  4. 所有者函数的 本地 范围(如果可用)
  5. ColdFusion 内置范围

如果我将某些内容定义为 'local.',它会仅搜索 1,还是 1,3 &4?

顺便说一句,我了解关闭和外.谁是所有者?

谢谢

<小时>

更新: 已提交增强请求:ColdFusion 10.0 - 功能 3191742

解决方案

 function helloTranslator(String helloWord){返回函数(字符串名称){返回#helloWord#,#name#";};}

这里 helloWordname 不能限定范围.有一个隐含的 Owner 范围,其中函数内定义的闭包"是声明(父)函数的本地范围,其中存在这些变量.所以这些变量必须是唯一的(在函数内)才能从闭包中访问.

在闭包中,搜索一个未作用域的变量:

  1. 闭包的本地范围
  2. 闭包的参数范围
  3. 外部/所有者函数的本地范围(如果可用)
  4. 外部/所有者函数的参数范围(如果可用)
  5. 变量范围(在创建闭包时可用)
  6. ColdFusion 内置范围

如果某些东西的范围是 Local,在闭包中,它只会在 1 中搜索.AFN 没有办法直接确定 3,4 的范围.

附言如前所述,Owner 作用域只不过是一个隐式作用域,它在创建闭包时指向父/外部函数本地作用域的缓存副本.

附言您可以使用 ColdFusion 记录增强功能 以明确此范围.

不同范围的示例如下.运行这个,你就会明白闭包如何使用不同的作用域.

任何函数 exampleClosureForm(arg1){函数 x(innerArg1,innerArg2){//var arg1= 50;//如果声明,将隐藏父级的 arg1writedump(arguments);//将转储闭包的writedump(local);//将转储闭包的写入转储(会话.a);//将是相同的会话共享写入转储(arg1);//将转储父母参数 arg1return session.a-- + innerArg1+innerArg2+arg1--;//递减以查看其连续调用的行为};写输出(x(1,2));writedump(arguments,"browser","html",false,"function");//会转储父母的writedump(local,"browser","html",false,"function");//会转储父母的arg1 = -100;//因为闭包共享父变量,这个变化应该得到反映返回 x;}会话.a = 10;a = exampleClosureForm(10);writeoutput("现在调用 <br>");writeoutput(a(innerArg1=5,innerArg2=5));写输出(
");//带有参数集合argsColl = structNew();argsColl.innerArg1=1;argsColl.innerArg2= 3;writeoutput(a(argumentCollection = argsColl));

Quoted from the Adobe ColdFusion 10: Using closures documentation:

function helloTranslator(String helloWord) 
{ 
    return function(String name) 
    { 
        return "#helloWord#, #name#"; 
    }; 
}

How to scope helloWord and name properly on the return line? Are they both in Arguments scope? If that's the case, they must be unique?

The Closures and functions section also mentions there are 4 more scopes on top of the already long scope search:

In closure, following is the order of search for an unscoped variable:

  1. Closure's local scope
  2. Closure's arguments scope
  3. Outer function's local scope if available
  4. Owner function's local scope if available
  5. ColdFusion built-in scope

If I scope something as 'local.', would it search 1 only, or 1,3 & 4?

BTW, I understand Closure & Outer. Who is Owner?

Thank you


Update: Enhancement Request filed: ColdFusion 10.0 - Feature 3191742

解决方案

    function helloTranslator(String helloWord) 
    { 
      return function(String name) 
      { 
        return "#helloWord#, #name#"; 
      }; 
    }

Here helloWord and name cannot be scoped. There is an implicit Owner scope with "closures defined within a function" which is the local scope of declaring (parent) function, where these variables are present. So these variables must be unique (within function) to be accessed from closures.

In closure, search for an unscoped variable goes through:

  1. Closure's local scope
  2. Closure's arguments scope
  3. Outer/Owner function's local scope if available
  4. Outer/Owner function's argument scope if available
  5. Variables scope (available at the time of closure's creation)
  6. ColdFusion built-in scope

If something is scoped as Local, in a closure, it would search in 1 only. AFN there is no way to directly scope for 3,4.

p.s. as said earlier Owner scope is nothing but an implicit scope which points to a cached copy of parent/outer function's local scope at the time of closure's creation.

p.s. You may log an enhancement with ColdFusion to make this scope explicit.

An example of different scopes is as follows. Run this and you shall understand how closure would use differnt scopes.

any function exampleClosureForm(arg1){

    function x(innerArg1,innerArg2){
        //var arg1= 50;// will hide parent's arg1 if declared
        writedump(arguments);// would dump closure's
        writedump(local);// would dump closure's
        writedump(session.a); // would be same session shared across
        writedump(arg1); // would dump parents argument arg1
        return session.a-- + innerArg1+innerArg2+arg1--;// decrementing to see its behavior for successive call
    };  
    writeoutput(x(1,2));
    writedump(arguments,"browser","html",false,"function");// would dump parent's
    writedump(local,"browser","html",false,"function");// would dump parent's
    arg1 = -100; // since closure is sharing the parent's variable, this change should get reflected
    return x;
}
session.a = 10;
a = exampleClosureForm(10);
writeoutput("now the calls <br>"); 
writeoutput(a(innerArg1=5,innerArg2=5)); 
writeoutput("<br>");
// with argumentcollection
argsColl = structNew();
argsColl.innerArg1= 1;
argsColl.innerArg2= 3;
writeoutput(a(argumentCollection = argsColl));

这篇关于如何在 CF10 中确定闭包变量的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆