Oh good, no change then.
you mean …there is no solution ?
No change from that initially proposed by me.
I’m confused …you have posted 2 approach. which is the the correct one for this ?
Cross that bridge when you get to it, not before.
The first one.
Ok…thanks. I’m checking that now…l’ll update result here in 5 minutes.
I recommend an adjustment, that simplifies the inner function.
That adjustment is where you use what is called a guard clause, to return out of the function early, which results in the rest of the function being less nested than before.
And, just a minor improvement, using is instead of already.
var confirmApprove = (function confirmApproveWrapper() {
var isApproved = false;
return function confirmApproveInner(contextPath) {
if (isApproved === true) {
return; // do not approve a second time
}
isApproved = true;
... // approve them
};
}());
I did a quick check with the first one…it worked.
I’ll check with the adjustment one soon and update here.
However …trying to understand your amazing code.
Does this variable not set to var isApproved = false; when calling second time ?
What’s happening is called a Closure. The confirmApproveWrapper function creates the isApproved variable, which is still accessible by the confirmApproveInner function, even after that inner function has been returned.
That confirmApproveInner function is allowed to fiddle about with the isApproved variable that the wrapper created.
Here’s an image that helps to show what’s going on:
[quote=“winzip, post:29, topic:256913, full:true”]
Does this variable not set to var isApproved = false; when calling second time ? is it a global variable ?
[/quote]The wrapper creates a local variable and sets it to false, but that’s the only time that that is done.
When the link is clicked only the inner function is being triggered, which only updates the isApproved variable to be true.
I have condensed the outer wrapper and invoking it together, using an IIFE (immediately invoked function expression) pattern to both create the function and invoke it at the same time.
var func = (function iife() {
...
}());
// the above is the same as
function iife() {
...
}
var func = iife();
my doubt is…
you calling confirmApprove
First time it creates var isApproved = false;
Second time you again calling confirmApprove but this time it does not do var isApproved = false;
It remembers value true . If it has to go through wrapper then it should set var isApproved = false; second time also…but it did not.
javascript is mysterious.
No, confirmApproved never creates isApproved.
When confirmApprove is called, it never goes through the wrapper. The wrapper is only done when confirmApprove is first created.
The sequence is:
- wrapper is invoked
- isApproved is created
- inner function is created
- inner function is returned
- inner function is assigned to confirmApprove
later when confirmApprove is invoked (from the link)
- isApproved is checked
- isApproved is then set to true
when wrapper is invoked ?
is it when page loads ? or when you call confirmApprove ?
When the page loads.
var confirmApprove = (function confirmApproveWrapper() {
var isApproved = false;
...
}()); // the () parenthesis invoke the wrapper function when the page loads.
wonderful… this is ok now. … this is a nice code. …enjoyed it.
you are very much helpful.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.