0

I have a function declared in this way:

function myFunction () {
    var something = 0;
    return something;
};

This function is called upon loading of a specific item in this way:

item.on("load", myFunction);

Initially, I considered doing this, but the function is not defined at the time that the item loads (it causes 'Uncaught ReferenceError: myFunction is not defined':

var mySomething = (function myFunction () {
    var something = 0;
    return something;
})();
item.on("load", myFunction); //ReferenceError happens here
console.log(mySomething);

How can I pass the information returned from the function into a variable? I would like to be able to use the variable 'mySomething' further down the line (illustrated by the print statement in this case).

user25976
  • 1,005
  • 4
  • 18
  • 39
  • Is this related to an asynchronous operation? Not really clear what *"function is not defined at the time"* means in real terms. Provide more specific details – charlietfl Jun 01 '18 at 02:34
  • Please clarify your question. It's unclear what result you're trying to achieve. – Aaronius Jun 01 '18 at 03:06
  • Does this help: [Storing named function in a variable with a different name](https://stackoverflow.com/q/45792417/4642212)? – Sebastian Simon Jun 01 '18 at 03:30

0 Answers0