I'm trying to understand when to use anonymous JavaScript functions.
我试图了解何时使用匿名JavaScript函数。
State differences between the functions? Explain when you would use each.
功能之间的状态差异?解释何时使用每个。
var test1 = function(){
$("<div />").html("test1").appendTo(body)
};
function test2() {
$("<div />").html("test2").appendTo(body)
}
I think the answer is that one uses anonymous function and the other doesn't to replace an empty div element. Does that seem right?
我认为答案是使用匿名函数而另一个不替换空div元素。那似乎对吗?
3 个解决方案
#1
6
In your example it really doesn't make a huge difference. The only difference is that functions declared using function foo() { }
are accessible anywhere within the same scope at any time, while functions declared using var foo = function () { }
are only accessible after the code that does the assignment has run.
在你的例子中,它确实没有太大的区别。唯一的区别是使用函数foo(){}声明的函数可以随时在同一作用域内的任何位置访问,而使用var foo = function(){}声明的函数只能在执行赋值的代码运行后才能访问。
foo(); // ok
function foo() { ... };
bar(); // error, bar is not a function
var bar = function () { ... };
bar(); // ok
You usually use anonymous functions in cases where you don't need a named function, or where you're constructing objects:
在不需要命名函数或构建对象的情况下,通常使用匿名函数:
arr.sort(function (a, b) { return a - b; }); // anonymous callback function
function MyObject() {
this.foo = function () { ... } // object constructor
}
#2
1
You would use a function like the following (which is another type of anonymous function) when you do not want to pollute the global namespace:
当您不想污染全局命名空间时,您将使用类似下面的函数(这是另一种类型的匿名函数):
(function() {
var pollution = 'blablabla';
function stinky() {
// Some code
}
})();
#3
1
You may check John Resig's Secrets of JavaScript Libraries, especially page 47 for JavaScript function. Quite lengthy, but you'll learn more about JavaScript
您可以查看John Resig的JavaScript库秘密,尤其是第47页的JavaScript函数。相当冗长,但您将了解有关JavaScript的更多信息
#1
6
In your example it really doesn't make a huge difference. The only difference is that functions declared using function foo() { }
are accessible anywhere within the same scope at any time, while functions declared using var foo = function () { }
are only accessible after the code that does the assignment has run.
在你的例子中,它确实没有太大的区别。唯一的区别是使用函数foo(){}声明的函数可以随时在同一作用域内的任何位置访问,而使用var foo = function(){}声明的函数只能在执行赋值的代码运行后才能访问。
foo(); // ok
function foo() { ... };
bar(); // error, bar is not a function
var bar = function () { ... };
bar(); // ok
You usually use anonymous functions in cases where you don't need a named function, or where you're constructing objects:
在不需要命名函数或构建对象的情况下,通常使用匿名函数:
arr.sort(function (a, b) { return a - b; }); // anonymous callback function
function MyObject() {
this.foo = function () { ... } // object constructor
}
#2
1
You would use a function like the following (which is another type of anonymous function) when you do not want to pollute the global namespace:
当您不想污染全局命名空间时,您将使用类似下面的函数(这是另一种类型的匿名函数):
(function() {
var pollution = 'blablabla';
function stinky() {
// Some code
}
})();
#3
1
You may check John Resig's Secrets of JavaScript Libraries, especially page 47 for JavaScript function. Quite lengthy, but you'll learn more about JavaScript
您可以查看John Resig的JavaScript库秘密,尤其是第47页的JavaScript函数。相当冗长,但您将了解有关JavaScript的更多信息