I'm learning Javascript at the moment, Please look at this snippet of code:
I've here defined 3 Immediately Invoked Function Expressions. My expectation here is when I console.log(controller) it returns undefined . Can anyone please explain me, why does controller IIFE be able to return an Object but not a value?
var budgetController = (function() { var x = 23; var add = function(a) { return x + a; } return { publicTest: function(b) { console.log(add(b)); } } })(); var UIController = (function() { //Some UI based Code })(); var controller = (function(budgetCtrl, UICtrl) { var z = budgetCtrl.publicTest(5); return z; })(budgetController, UIController); console.log(controller); // Should return the value of 'z' [My Confusion/Misconception lies here] console.log(budgetController); // Returns a Object
since you are passing params to publicTest, you should return the value to store it in Z variable
var budgetController = (function() { var x = 23; var add = function(a) { return x + a; }; return { publicTest: function(b) { console.log(add(b)); return add(b); } }; })(); var UIController = (function() { //Some UI based Code })(); var controller = (function(budgetCtrl, UICtrl) { var z = budgetCtrl.publicTest(5); return z; })(budgetController, UIController); console.log(controller); // Should return the value of 'z' [My Confusion/Misconception lies here] console.log(budgetController);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.