如何在CF10中扩展闭包的变量

如何在CF10中扩展闭包的变量

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

问题描述

引用自部分还提到在已经很长的范围搜索之上有4个范围:

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

在闭包中,以下是搜索无范围变量的顺序:


  1. Closure的 local scope


  2. local local scope

  3. li>
  4. ColdFusion内置范围

  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


'local。',它只搜索1,或1,3& 4?

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

BTW,我明白Closure&外。谁是所有者

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

谢谢

更新:提交的增强请求已提交:

Update: Enhancement Request filed: ColdFusion 10.0 - Feature 3191742

推荐答案

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

这里 helloWord name 不能作用域。有一个隐式的 Owner 作用域,在函数中定义了闭包,这是声明(父)函数的局部作用域,这些变量存在。所以这些变量必须是唯一的(在函数内)从闭包访问。

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. 外部/所有者函数的参数范围


  2. 变量范围(在创建闭包时可用)

  3. ColdFusion内置范围

  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

如果某个作用域的范围为 Local ,在闭包中,它将仅搜索1。 AFN没有办法直接覆盖3,4。

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。如前所述 Owner scope只是一个隐式作用域,指向在创建闭包时父/外层函数的局部作用域的缓存副本。

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。您可以,以使此范围明确。

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中扩展闭包的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:39