Prev: Request for DatePart test in Win7; need only read a Web page
Next: prnmngr vs delete all network printers
From: Bob Barrows on 24 Feb 2010 14:37 mayayana wrote: >> No, a closure is a function that retains/keeps (part of) the >> context it was created in. When such a function is called later, >> this context can be used. In that way you get a tight association >> between data (information) and behavior (code) - a closure is >> like an object with just one method. >> > > Your description sounds > like a Static variable in VB, which holds its > assigned value between calls. But a static > variable is not functionally different from a > global variable. It just has a scope local to > the function it's in. From http://www.jibbering.com/faq/faq_notes/closures.html: Closures are one of the most powerful features of ECMAScript (javascript) but they cannot be property exploited without understanding them. They are, however, relatively easy to create, even accidentally, and their creation has _potentially harmful consequences, particularly in some relatively common web browser environments_. To avoid accidentally encountering the drawbacks and to take advantage of the benefits they offer it is necessary to understand their mechanism. This depends heavily on the role of scope chains in identifier resolution and so on the resolution of property names on objects. The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. And that those inner functions are allowed access to all of the local variables, parameters and declared inner functions within their outer function(s). _A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained, so that it may be executed after the outer function has returned_. At which point it still has access to the local variables, parameters and inner function declarations of its outer function. Those local variables, parameter and function declarations (initially) have the values that they had when the outer function returned and may be interacted with by the inner function. -- HTH, Bob Barrows
From: ekkehard.horner on 24 Feb 2010 14:52 mayayana schrieb: >> No, a closure is a function that retains/keeps (part of) the >> context it was created in. When such a function is called later, >> this context can be used. In that way you get a tight association >> between data (information) and behavior (code) - a closure is >> like an object with just one method. >> > > Your description sounds > like a Static variable in VB, which holds its > assigned value between calls. But a static > variable is not functionally different from a > global variable. It just has a scope local to > the function it's in. > Is a Closure basically a function within > a function that has a static variable? To put some code to the abstract explanations in http://rosettacode.org/wiki/Closures http://en.wikipedia.org/wiki/Closure_(computer_science) I wrote this .js script to demonstrate (very simple) closures: // A 'normal' JS function returning a value; it can be called // by writing its name followed by parameter () around its // parameter(s) literally in the code, or treated/accessed as data // ('pointer' to function) by using just its name (without ()). // The function is created at 'compile time' (=just before the // program starts to execute). function addSeven( nNum ) { return 7 + nNum; } // Function that builds a context (nSummand1) from its parameter // and returns the newly created anonymous function. The returned // functions are created at runtime (you could use user input/runtime // data to influence their creation/behavior). function makeAdd( nParm ) { var nSummand1 = nParm + 1; return function( nSummand2 ) { return nSummand1 + nSummand2 }; } // Treat functions as data: put two closures and a 'normal' function // into an array. (You could assign them to variables, pass them as // parameters to other functions ...; instead of hardcoding 4 and 5, // you could ask the user or look into a database ... ) var aFuncs = [ makeAdd( 4 ), makeAdd( 5 ), addSeven ]; // Using them repeatedly for (var nNum = 1; nNum < 4; ++nNum ) { WScript.Echo( "fncAddFive( " + nNum + " ) => " + aFuncs[ 0 ]( nNum ) ); WScript.Echo( " fncAddSix( " + nNum + " ) => " + aFuncs[ 1 ]( nNum ) ); WScript.Echo( " addSeven( " + nNum + " ) => " + aFuncs[ 2 ]( nNum ) ); WScript.Echo( " addSeven( " + nNum + " ) => " + addSeven( nNum ) ); WScript.Echo(); } output: fncAddFive( 1 ) => 6 fncAddSix( 1 ) => 7 addSeven( 1 ) => 8 addSeven( 1 ) => 8 fncAddFive( 2 ) => 7 fncAddSix( 2 ) => 8 addSeven( 2 ) => 9 addSeven( 2 ) => 9 fncAddFive( 3 ) => 8 fncAddSix( 3 ) => 9 addSeven( 3 ) => 10 addSeven( 3 ) => 10 So instead of writing many functions like addSeven literally in your code, you define the function/functionality just once: ... { return nSummand1 + nSummand2 } ... and can create hundreds of variants at runtime. [...] > I understood that, but as I posted before it's > a superfluous function that makes no sense. You miss the point: Eval and Execute take *Strings* (runtime data) as input. > There's no ambiguity in context. > You used this relatively complex code: > > sExpr = "nA = nB" You could use InputBox here to fill sExpr. (Think of a function plotter program that lets the user specify an expression and Evals that for (a range of) parameters.) > bRes = EVal( sExpr ) > WScript.Echo sExpr, "=>", CStr( bRes ) > [...]
From: ekkehard.horner on 24 Feb 2010 15:10 Bob Barrows schrieb: > mayayana wrote: >>> No, a closure is a function that retains/keeps (part of) the >>> context it was created in. When such a function is called later, >>> this context can be used. In that way you get a tight association >>> between data (information) and behavior (code) - a closure is >>> like an object with just one method. >>> >> Your description sounds >> like a Static variable in VB, which holds its >> assigned value between calls. But a static >> variable is not functionally different from a >> global variable. It just has a scope local to >> the function it's in. > > Every discussion of closures I've previously seen centered on how they > were typically unintentionally created, as well as the adverse effects > when they are created (memory leaks, IIRC). There was little discussion > as to their desirability or usefulness. I will need to research what > Ekkehard referred to. > A closure links/combines functionality and data - just what OOP is all about. Think of it as a lightweight object. (So it's not by chance that Bruce used a class to implement something like a closure (function composition) in VBScript.)
From: mayayana on 24 Feb 2010 15:16 > > If there's no better usage than what you used > > as an example then personally I would conclude > > that there is, indeed, no purpose to Eval in VBS. > > I suppose you've read what Eric Lippert has had to say about this? ;-) > Generally I'm not very interested in what Eric Lippert has to say about anything, but if you thought it was worthwhile then I'd be curious to see it.
From: mayayana on 24 Feb 2010 15:20 Thanks for that. It ouinds pretty much like the kind of thing I was finding with a Google search and a wikipedia read. Sounds like a can of beans to me, but I'll leave it to the javascripters to decide, since a wrapped function is not something I deal with in VBS or VB. :) > > From http://www.jibbering.com/faq/faq_notes/closures.html: > Closures are one of the most powerful features of ECMAScript > (javascript) but they cannot be property exploited without understanding > them. They are, however, relatively easy to create, even accidentally, > and their creation has _potentially harmful consequences, particularly > in some relatively common web browser environments_. To avoid > accidentally encountering the drawbacks and to take advantage of the > benefits they offer it is necessary to understand their mechanism. This > depends heavily on the role of scope chains in identifier resolution and > so on the resolution of property names on objects. > > The simple explanation of a Closure is that ECMAScript allows inner > functions; function definitions and function expressions that are inside > the function bodes of other functions. And that those inner functions > are allowed access to all of the local variables, parameters and > declared inner functions within their outer function(s). _A closure is > formed when one of those inner functions is made accessible outside of > the function in which it was contained, so that it may be executed after > the outer function has returned_. At which point it still has access to > the local variables, parameters and inner function declarations of its > outer function. Those local variables, parameter and function > declarations (initially) have the values that they had when the outer > function returned and may be interacted with by the inner function. > -- > HTH, > Bob Barrows > >
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Request for DatePart test in Win7; need only read a Web page Next: prnmngr vs delete all network printers |