Closures javascript vs java -
i learning javascript , came across following code snippet:
var outervalue = true; function outerfn(){ assert( outerfn && outervalue, "these come closure." ); }
insofar understand closures in above context, allow outerfn see outervalue variable.
my question is: how different other programming language - such java instance? expected outervalue's scope allow outerfn see it.
added later on:
var outervalue = true; function outerfn() { console.log(outervalue); } function anotherfunction(arg){ console.log("anotherfunction"); arg.call(this); } anotherfunction(outerfn);
is better example of closure?
your example not illustrate difference, not define scope of outervalue. in javascript, may nest functions arbitrarily within 1 another, , closures make sure inner functions can see outer functions when invoked after outer functions no longer in scope.
in java, nesting functions not (yet) legal, , closures not come play. having class field in place of outervalue
, class method in place of function different, field of course associated scope of class, not method.
Comments
Post a Comment