闭包和嵌套闭包有什么区别?

时间:2009-06-04 12:42:15

标签: closures

闭包和嵌套闭包有什么区别?对示例的一个很好的解释会有所帮助。

1 个答案:

答案 0 :(得分:3)

变量的范围以及它们所绑定的环境。

如何难以在编译器中实现:)

ClosureA可能绑定到它的本地范围LA和父范围PA,然后一个名为ClosureB的闭包被绑定到(潜在地)LB,LA,PA

func a( v1,v2,v3 ){
    closure_b(bv1, bv2, bv3) { # Closure
        b_local1 = bv1
        b_local2 = v1  # parent scope

        closure_c(cv1, cv2) { # Nested closure has 'closure_b's scope too
            c_local1 = cv1
            c_local2 = bv1 # direct-parent scope
            c_local3 = v1 # parent's parent scope (nesting)
            c_local4 = b_local2
        }
        return closure_c;

    }
    return closure_b(); # closure_b() returns closure_c 
}