1

I have an AJAX request and in the success handler, to be able to grab the data anywhere I pass it to a function, called dataHandler. From there I handle what I need and end up with an array of items.

function dataHandler() {
    var myArray = ["21/06/2016"];
}

I'm trying to pass what i've got in the dataHandler() function to another function that is self invoking but if I named the function like $(function myName(){} then in my first function did myName(myArray); I still can't access the data.

$(function() {
    console.log(myArray);
})

How can I pass a variable from a normal function to a self invoking function?

bwegs
  • 3,769
  • 2
  • 30
  • 33
pourmesomecode
  • 4,108
  • 10
  • 46
  • 87
  • Make it global, and get access of it –  Jun 21 '16 at 14:48
  • You might actually want to invoke the function: `(function() { /.../ })()`? This works regardless of jQuery, just create a function, wrap it in parenthesis and invoke it using the standard function call. Also, the value `myArray` does not exist )outside of the scope_. – somethinghere Jun 21 '16 at 14:48
  • 1
    You don't have any IIFE in your code. You are calling `$()` immediately and passing it a function as an argument. – Quentin Jun 21 '16 at 14:49
  • How would I make it global? creating a globar variable then pushing my array to the global variable in the first function then grabbing it from the second? It's not sync though so it might not always work? – pourmesomecode Jun 21 '16 at 14:49
  • Calling `dataHandler` _returns_ that array, but the returned array needs to be stored or passed to something or it disappears into the void, as the variable `myArray` only exists _inside_ the function, not outside it. So `console.log(dataHandler());` will log what you want, as well as `var a = dataHandler(); console.log(a);`, but not `dataHandler() console.log(myArray)`because `myArray` is `undefined` – somethinghere Jun 21 '16 at 14:52
  • "Calling dataHandler returns that array: — It doesn't. There's no return statement. Perl returns the result of evaluating the last statement in a sub, but JavaScript doesn't. JS needs an explicit return statement. – Quentin Jun 21 '16 at 14:53
  • If I `console.log` dataHandler() from within my other first, I get undefined – pourmesomecode Jun 21 '16 at 14:53
  • @Quentin ow woops, entirely read over that. – somethinghere Jun 21 '16 at 14:54

3 Answers3

4

You don't have any IIFE in your code, and the variable you want to pass is locally scoped to the function it is declared in.

It sounds like you actually need something like:

function dataHandler() {
    var myArray = ["21/06/2016"];

    $(function() {
        console.log(myArray);
    });
}

dataHandler();

Where:

  • dataHandler is actually called (so the variable gets assigned)
  • $() is called inside dataHandler so that myArray is in scope when the anonymous function is passed to it
Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Although the function inside there is not really doing anything is it? Could you replace `$(fu...)` with just `console.log(myArray)` and you would achieve the same effect, right? Besides the point, I know. – somethinghere Jun 21 '16 at 14:53
  • 2
    @somethinghere — It delays the execution of it until the ready event fires.That isn't really significant for a console.log, but would be for anything involving the DOM. – Quentin Jun 21 '16 at 14:54
  • @somethinghere Inquisitive minds help everyone around them learn as well, keep it up - case in point [I learned something new](http://stackoverflow.com/questions/7614574/dollar-sign-before-self-declaring-anonymous-function-in-javascript) and unrelated to the question. – bwegs Jun 21 '16 at 19:39
2

Pass the function as an argument to the self invoking function, like this:

function dataHandler() {
    var myArray = ["21/06/2016"];
    return myArray;
}

(function(yourArray) {
    console.log(yourArray);
})(dataHandler());
0

Usually the module pattern is implemented using self-invoking functions. In the module pattern, we keep the reference to the function’s returned object. In such case we consider the return value as the public API of the module:

var module = (function (param) {
  // private
  return {
    // public
  };
}(param));
Jitendra
  • 3,135
  • 2
  • 26
  • 42