Reputation: 12718
I want to use a closure in myObj
so I can increment myIndex
without having to add myIndex
to the global namespace (aka, in TaskHandler
).
That works. But I also need to pass in myValue
to the closure. I thought passing it through (function (param) { })(myValue);
was the way to do it. But it's undefined.
TaskHandler.myFunction(value);
TaskHandler = {
myFunction : function (value) {
this.myObj.run(value);
},
myObj : {
run : function (value) {
this.doIt(value);
},
doIt : (function (value) {
var myIndex = 0;
return function () {
myIndex++;
doSomethingWithValue(myIndex, value); //value undefined
}
})(value)
},
};
Upvotes: 0
Views: 49
Reputation: 141827
The function your IIFE returns should accept the value argument, not the IIFE itself:
doIt : (function () {
var myIndex = 0;
return function (value) {
myIndex++;
doSomethingWithValue(myIndex, value);
}
})()
Upvotes: 2