JavaScript 中的匿名函数和内联函数有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19159703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between anonymous and inline functions in JavaScript?
提问by matori82
The title sums up my question. An example that demonstrates the point would be nice.
标题总结了我的问题。一个证明这一点的例子会很好。
回答by Daniel Gimenez
First off, there doesn't seem to be a consensus definition for inline functions in JavaScript. I consider an inlinefunction to be a special case of a JavaScript function. An inlinefunction is a function assigned to a variable that is created at runtime instead of at parsetime.
首先,对于 JavaScript 中的内联函数似乎没有一致的定义。我认为内联函数是 JavaScript 函数的特例。一个内联函数是分配给在运行时,而不是在分析时创建的变量的函数。
Anonymous functions and inline functions are practically the same, in that they are created at runtime. The difference is that an inline function is assigned to a variable and so it can be reused. In that way, inline functions work the same as a regular function.
匿名函数和内联函数实际上是相同的,因为它们是在运行时创建的。不同之处在于内联函数被分配给一个变量,因此它可以被重用。这样,内联函数的工作方式与常规函数相同。
Function
功能
function func() {
alert ('function');
}
$('a').click(func);
Inline Function
内联函数
var func = function() {
alert ('inline')
};
$('a').click(func);
Anonymous Function
匿名函数
$('a').click(function() {
alert('anonymous');
});
Anonymous and inline functions can have performance penalties versus a regular function. See var functionName = function() {} vs function functionName() {}.
与常规函数相比,匿名函数和内联函数可能会降低性能。请参阅var functionName = function() {} 与 function functionName() {}。
回答by CSStudent
Inline function is somewhat different , quoting from wikipedia :
内联函数有些不同,引用自维基百科:
an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.
内联函数是要求编译器对其进行内联扩展的函数。换句话说,程序员要求编译器在调用函数的每个地方插入函数的完整主体,而不是在定义函数的地方生成代码来调用函数。编译器没有义务尊重这一要求。
Javascript does not support the concept of inline function. But i found some references in web where callbacks like:
Javascript 不支持内联函数的概念。但我在网络中发现了一些参考资料,其中回调如:
(function(){
setTimeout(/*inline function*/function(){ /*some code here*/ }, 5);})
();
are called inline function. As you can see these function do not have any name either , so they are essentially same as anonymous function. I think ,since you are defining the function where you are using it ,it's been referenced by inline function but the name "anonymous function" best describes the notion.
被称为内联函数。如您所见,这些函数也没有任何名称,因此它们本质上与匿名函数相同。我认为,由于您正在定义使用它的函数,因此它已被内联函数引用,但名称“匿名函数”最能描述该概念。
回答by jhnlsn
Anonymous functions are defined like this
匿名函数是这样定义的
var x = 1;
(function(x){
// Do something
console.log(x); // 1
})(x);
However, the definition of inline function is a bit unclear to me.
但是,内联函数的定义对我来说有点不清楚。
回答by Chokchai
Inline function
内联函数
var foo = function (){
alert('Hello')
}
setTimeout(foo, 100);
Anonymous function
匿名函数
setTimeout(function(){
alert('Hello')
}, 100);
They are doing the same thing, but inline function is better when you want to reuse it. Anonymous is good for one-time use because you do not need to worry if its name will conflict with other variables, and it's shorter.
他们在做同样的事情,但是当你想重用内联函数时,它会更好。Anonymous 适合一次性使用,因为您不必担心它的名称是否会与其他变量冲突,而且它更短。
So it depends on your situation.
所以这取决于你的情况。