我想知道是否有任何方法可以从函数外部访问函数中闭包所捕获的变量; 例如,如果我有:
A = function(b) {
var c = function() {//some code using b};
foo: function() {
//do things with c;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法c在一个实例中访问A.就像是:
var a_inst = new A(123);
var my_c = somejavascriptmagic(a_inst);
Run Code Online (Sandbox Code Playgroud) 进入一个有趣的问题; 以下类编译:
public class Test {
public static void main(String[] args) throws Exception {
A a = new A();
B b = new B();
foo(a);
foo(b);
}
private static void foo(A a) {
System.out.println("In A");
}
private static void foo(B b) {
System.out.println("In B");
}
private static class A {}
private static class B extends A {}
}
Run Code Online (Sandbox Code Playgroud)
但是这个失败了:
public class Test {
public static void main(String[] args) throws Exception {
A<String> a = new A<>();
B b = new …Run Code Online (Sandbox Code Playgroud)