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 valuemyArray
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 variablemyArray
only exists inside the function, not outside it. Soconsole.log(dataHandler());
will log what you want, as well asvar a = dataHandler(); console.log(a);
, but notdataHandler() console.log(myArray)
becausemyArray
isundefined
– somethinghere Commented Jun 21, 2016 at 14:52
3 Answers
Reset to default 4You 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 insidedataHandler
so thatmyArray
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
版权声明:本文标题:javascript - Passing a variable to a self invoking function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742320166a2452625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论