0

I can't figure out how to access pubOuter, can anyone figure this one out? It keeps returning undefined, because I can't find a way how pass outer from inner2() function. Or I'm missing something obvious. Many thanks.

Javascript

var myObject = (function(){
   var outer;

   function inner1(IN_number){
     outer = IN_number*2;
   }

   function inner2(){
     inner1(10);
     return outer;
   }


   return {
     pubFnInner1: inner1,
     pubFnInner2: inner2,
     pubOuter:    outer
   };

})();

$("#click").on("click", function(){
   console.log("outer" + myObject.pubOuter);  
});

HTML

<button id="click">Click</button>
5
  • 5
    We need more information. From what I see without a function call to inner1, then outer will be undefined. To be clear, you need to call it before the return {...}, afterwards outer will be out of scope and no longer able to be edited. Commented Jul 27, 2016 at 19:43
  • 1
    otherwise call inner2(); before the return statement. Commented Jul 27, 2016 at 19:50
  • That's what happens when u work excessive hours!!! I missed the call to function:) Thanks Spencer! Commented Jul 27, 2016 at 19:50
  • the original code is too complex, I've just made some rough approximation. Thanks guys! Commented Jul 27, 2016 at 19:51
  • I'm not a moderator but, if you could, please post your answer mark it as the answer. Commented Jul 27, 2016 at 19:52

2 Answers 2

1

You are successfully accessing the putOuter property. The problem is that it hasn't been set to anything before you're using it.

I've modified the code to initialize outer to 1. Otherwise, when you multiply it by IN_number in the inner1 function, it will result in NaN.

var myObject = (function(){
   var outer = 1;

   function inner1(IN_number){
     outer = IN_number*2;
   }

   function inner2(){
     inner1(10);
     return outer;
   }


   return {
     pubFnInner1: inner1,
     pubFnInner2: inner2,
     pubOuter:    outer
   };

})();

$("#click").on("click", function(){

   console.log("outer" + myObject.pubOuter);
   myObject.pubFnInner1(10);
   console.log("outer" + myObject.pubOuter);

   // or combine both steps by using your pubFnInner2 method.
   console.log("outer" + myObject.pubFnInner2());
});
Sign up to request clarification or add additional context in comments.

Comments

0

pubOuter is undefined because you're assigning it to outer which is undefined because you never run inner1, you need to do this:

var myObject = (function(){
   var outer;

   function inner1(IN_number){
     outer = IN_number*2;
   }

   function inner2(){
     inner1(10);
     return outer;
   }


   return {
     pubFnInner1: inner1(),
     pubFnInner2: inner2(),
     pubOuter:    outer
   };

})();

$("#click").on("click", function(){
   console.log("outer" + myObject.pubOuter);  
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.