Closing over "this".
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
class Foo {
|
||||
getClosure {
|
||||
return fn {
|
||||
return this.toString
|
||||
}
|
||||
}
|
||||
|
||||
toString { return "Foo" }
|
||||
}
|
||||
|
||||
var closure = (new Foo).getClosure
|
||||
io.write(closure.call) // expect: Foo
|
||||
@@ -0,0 +1,22 @@
|
||||
class Outer {
|
||||
method {
|
||||
io.write(this.toString) // expect: Outer
|
||||
|
||||
fn {
|
||||
io.write(this.toString) // expect: Outer
|
||||
|
||||
class Inner {
|
||||
method {
|
||||
io.write(this.toString) // expect: Inner
|
||||
}
|
||||
toString { return "Inner" }
|
||||
}
|
||||
|
||||
(new Inner).method
|
||||
}.call
|
||||
}
|
||||
|
||||
toString { return "Outer" }
|
||||
}
|
||||
|
||||
(new Outer).method
|
||||
@@ -0,0 +1,16 @@
|
||||
class Foo {
|
||||
getClosure {
|
||||
return fn {
|
||||
return fn {
|
||||
return fn {
|
||||
return this.toString
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toString { return "Foo" }
|
||||
}
|
||||
|
||||
var closure = (new Foo).getClosure
|
||||
io.write(closure.call.call.call) // expect: Foo
|
||||
Reference in New Issue
Block a user