foo = { x = 3 }
function foo:bar(baz)
return function(thud)
return thud + baz + self.x
end
end
womble = foo:bar(4)
womble(7) --> 14
Python I believe also closes over self as a variable, and Ruby has similar behavior for local procs, even though « self » is a special form in Ruby.This is in distinct contrast to JavaScript, where the value of the special form « this » goes nuts inside closures because it's attached to the function:
foo = { x: 3 };
foo.bar = function(baz) {
return function(thud) {
return thud + baz + this.x;
};
};
womble = foo.bar(4);
womble(7) # --> NaN
So in what way are you placing Lua and JavaScript together, and Python and Ruby together?[slightly rearranged for clarity]