Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access an object form another function

i wanna get access to localVariable form another function

(sorry about grammar English is not my main language)

the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let Info = {}
function storeInfo(Info) {
    let name;
    let age;
    function getName() {
        name = prompt('enter your name:')
    }
    function getAge() {
        age = prompt('how old are you?')
    }
    getName();
    getAge(); 
    Info = {
        name:name,
        age:age,
    }

    returnInfo(Info)
}

storeInfo(Info)

function returnInfo(Info) {
    return Info.name + ' ' + Info.age;
}
let result = returnInfo(Info);
alert(result)

i wanna returnInfo function get the info of storeInfo function and alert it and the end

>Solution :

I simplified it for you because you made a lot of mess.

  1. Put the functions as first in code, it will be more clear.
  2. Try not to mix functions with executable code at the learning stage.
  3. To use function in function better is to learn about classes.

Simplified code:

    function storeInfo() {
      let localFuncInfo = {
        name:prompt('enter your name:'),
        age:prompt('how old are you?'),
      }
      return localFuncInfo;
    }
    
    function returnInfo(Info) {
      return Info.name + ' ' + Info.age;
    }

    let Info = storeInfo() ;
    let result = returnInfo(Info);
    
    alert(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading