admin管理员组文章数量:1192347
I'd like to use an inline function with arguments to set a variable. Here is a boiled down version (which apparently is only pseudo code at this point) of what I'm attempting:
var something = 10;
var something_else = 15;
var dynamic_value = (function(something,something_else){
if(something == something_else){
return "x";
}else{
return "y";
}
})();
In this case, "dynamic_value" should be "y". The problem is that the variables "something" and "something_else" are never seen inside of this inline function.
How do you send arguments to an inline function?
edit: I'm using jquery, although that may not really apply to this question.
I'd like to use an inline function with arguments to set a variable. Here is a boiled down version (which apparently is only pseudo code at this point) of what I'm attempting:
var something = 10;
var something_else = 15;
var dynamic_value = (function(something,something_else){
if(something == something_else){
return "x";
}else{
return "y";
}
})();
In this case, "dynamic_value" should be "y". The problem is that the variables "something" and "something_else" are never seen inside of this inline function.
How do you send arguments to an inline function?
edit: I'm using jquery, although that may not really apply to this question.
Share Improve this question asked Dec 21, 2012 at 17:43 Flat CatFlat Cat 9164 gold badges13 silver badges24 bronze badges3 Answers
Reset to default 20Send them in when invoking the function
var dynamic_value = (function(something, something_else) {
...
})(value_for_something, value_for_something_else);
You will need to call it like this.
var dynamic_value = (function(something,something_else){
if(something == something_else){
return "x";
}else{
return "y";
}
})(something,something_else);
The reason for this is when you are defining the same names in the function parameter, they are just the names of the parameters, the variables don't get inserted there. The last line is call to the function where actual variables are passed.
Besides, you have just created a closure. The closure has access to all the variables declared in the function that contains it. Another interesting fact in this code is that variables defined at the containing function level are getting shadowed by the variables that are part of the closure function. The reason is obvious: the variable names at the closure is same as the variable names at the containing function.
I'm going to guess here that you want dynamic_value
to just bind to something
but not something_else
.
var base_value = 10;
var something_else = 15;
var dynamic_value = (function(base_value){
return function (compare) {
if(base_value == compare){
return "x";
} else {
return "y";
}
};
})(base_value);
alert(dynamic_value(something_else)); // "y"
alert(dynamic_value(10)); // "x"
本文标签: javascriptPassing Arguments into an Inline FunctionStack Overflow
版权声明:本文标题:javascript - Passing Arguments into an Inline Function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738425788a2086109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论