admin管理员组

文章数量:1332872

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?

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?

Share Improve this question edited Jun 21, 2016 at 14:47 bwegs 3,7672 gold badges32 silver badges34 bronze badges asked Jun 21, 2016 at 14:46 pourmesomecodepourmesomecode 4,36812 gold badges53 silver badges91 bronze badges 8
  • Make it global, and get access of it – user2628521 Commented Jun 21, 2016 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 Commented Jun 21, 2016 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 Commented Jun 21, 2016 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 Commented Jun 21, 2016 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 Commented Jun 21, 2016 at 14:52
 |  Show 3 more ments

3 Answers 3

Reset to default 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

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());

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));

本文标签: javascriptPassing a variable to a self invoking functionStack Overflow