Home  >  Article  >  Web Front-end  >  JS anonymous function example analysis

JS anonymous function example analysis

高洛峰
高洛峰Original
2016-12-05 10:48:16899browse

The examples in this article describe JS anonymous functions. Share it with everyone for your reference, the details are as follows:

/* 匿名函数*/
(function() {
var foo = 10;
var bar = 2;
alert(foo * bar);
})();
/* 匿名函数,带参数 */
(function(foo, bar) {
alert(foo * bar);
})(10, 2);
/* 匿名函数返回值 */
var baz = (function(foo, bar) {
return foo * bar;
})(10, 2);
// baz will equal 20.
/* 匿名函数关闭 */
var baz;
(function() {
var foo = 10;
var bar = 2;
baz = function() {
return foo * bar;
};
})();
baz();


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn