admin管理员组文章数量:1335871
I am new in Javascript, but have deep background real OO languages like C#, Java, C++... In Javascript there is a concept called anonymous functions. Here is a sample code:
( function() {
for(var x = 0;x<5;x++) {
console.log(x);
}
})();
As I have understood the parantheses at the end make the function call itself. There is also another syntax which does the same:
var x = function() {
for(var x = 0;x<5;x++) {
console.log(x);
}
}();
But right now if I try to use x
, it does not execute the function again. So what is the goal if using the assignment in the second version? Can I use the function via x
again?
I am new in Javascript, but have deep background real OO languages like C#, Java, C++... In Javascript there is a concept called anonymous functions. Here is a sample code:
( function() {
for(var x = 0;x<5;x++) {
console.log(x);
}
})();
As I have understood the parantheses at the end make the function call itself. There is also another syntax which does the same:
var x = function() {
for(var x = 0;x<5;x++) {
console.log(x);
}
}();
But right now if I try to use x
, it does not execute the function again. So what is the goal if using the assignment in the second version? Can I use the function via x
again?
-
3
Since you have
()
after function, it will call it immediately and assign its return value (in your case, undefined). Remove()
and if you want to call it immediately, make a call on next line like:x();
– Rajesh Commented Nov 6, 2017 at 7:54 - 1 Beware that JavaScript is not object oriented. Instead, JS is prototype oriented and there will be things that surprise you. – Derek 朕會功夫 Commented Nov 6, 2017 at 7:58
- 2 why someone downvoted this question? it is a good question for beginners – Luke Commented Nov 6, 2017 at 7:59
-
The first one without
var x =
is actually an IIFE stackoverflow./questions/8228281/… – Niladri Commented Nov 6, 2017 at 8:03 -
1
var x is the result of calling that anonymous function ... which, in this case ... is
undefined
as it has no return – Jaromanda X Commented Nov 6, 2017 at 8:06
3 Answers
Reset to default 5Self executing function are known as IIFE (Immediately-Invoked Function Expression), it is usually used to control the scoping so you don't end up with a lot of global variables.
For example, this function act as a moneybox, it encapsulate all information of your "money", so you can only insert money or get the total money, but you can't directly call add/get and access the variable.
It can be used as a form of OOP as well, since you are already very familiar with it
var myMoneyBox = (function() {
var money = 0;
function addMoney(x) {
if (x > 0)
money += x;
}
function getMoney() {
return money;
}
return {
add: addMoney,
get: getMoney
}
})();
myMoneyBox.add(10);
console.log(myMoneyBox.get());
x
is assigned the result of your function, just like in any other expression x = f()
, you just define f
within the expression. It doesn't have a return value so in this case the value of x
is undefined
.
If you want to use x
to call the function, then just don't add the ()
to the end of the expression...
I think this could help you:
var x = function() {
for(var x = 0;x<5;x++) {
console.log(x);
}
};
x();
本文标签: About Anonymous Functions in JavascriptStack Overflow
版权声明:本文标题:About Anonymous Functions in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742394295a2466608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论