Pulling my hair out here trying to understand the infuriating nuances of Javascript. Hopefully some JS guru can take a gander, point and go, "well, there's yer problem...".
Here's a slimmed down sample of the problem:
var Parent = (function () {
var func1 = function () {
func2(function (res) {
console.log(res);
});
};
var func2 = function (callback) {
callback('abc');
};
return {
init: function () {
func1();
func2();
}
};
})();
Call with Parent.init();
This fails with the error:
Uncaught TypeError: callback is not a function
at check2 (<anonymous>:9:9)
at Object.init (<anonymous>:15:13)
at <anonymous>:1:8
What's getting me, is that if I comment out the enclosing code, like so, then it works as expected:
// var Parent = (function () {
var func1 = function () {
func2(function (res) {
console.log(res);
});
};
var func2 = function (callback) {
callback('abc');
};
// return {
// init: function () {
// func1();
// func2();
// }
// };
// })();
...and call with func1();
Result:
abc
What am I missing?
Thanks
func2();what do you expectcallbackto be and why?init()function, you're callingfunc2();, without specifying thecallbackfunction as the required argument. In your second version (withinitcommented out), you're correctly specifying thecallbackfunction infunc2(function (res) { ... });.func2()must do some processing and return a result. My (perhaps screwed) understanding is thatcallbackmust call the "calling" anon function infunc1(). lol, I dunno, hence the question. I just want to know why it works in the one sample, but not the other.