Javascript中没有名称的函数与名称和函数之间的区别 [英] Difference between function with a name and function without name in Javascript
本文介绍了Javascript中没有名称的函数与名称和函数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1。
function abc(){
alert("named function");
}
v / s
2.
function(){
alert("Un-Named function");
}
请从初学者点解释。
推荐答案
它们的工作方式完全相同。只有你能够如何运行它们才是不同的。
They work exactly the same. It's only in how you are able to run them that they are different.
所以示例#1你可以随时用 abc再次呼叫( );
。例如2,你要么必须将它作为参数传递给另一个函数,要么设置一个变量来存储它,如下所示:
So example #1 you could call again at any point with abc();
. For example 2, you would either have to pass it as a parameter to another function, or set a variable to store it, like this:
var someFunction = function() {
alert("Un-Named function");
}
以下是如何将其传递到另一个函数并运行它。
Here's how to pass it into another function and run it.
// define it
function iRunOtherFunctions(otherFunction) {
otherFunction.call(this);
}
// run it
iRunOtherFunctions(function() {
alert("I'm inside another function");
});
正如David在下面提到的那样,你也可以立即拨打它:
As David mentions below, you can instantly call it too:
(function() {
alert("Called immediately");
})(); // note the () after the function.
这篇关于Javascript中没有名称的函数与名称和函数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文