JavaScript中的匿名和内联函数有什么区别? [英] What is the difference between anonymous and inline functions in JavaScript?

查看:1069
本文介绍了JavaScript中的匿名和内联函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题总结了我的问题。一个演示这一点的例子会很好。

解决方案首先,内联似乎没有共识定义在JavaScript中的功能。我认为一个 inline 函数是JavaScript函数的特例。 inline 函数是分配给在运行时而不是parsetime创建的变量的函数。

匿名函数和内联函数实际上是相同的,因为它们是在运行时创建的。不同的是内联函数被分配给一个变量,因此它可以被重用。这样,内联函数和普通函数一样工作。



函数



  function func(){
alert('function');
}

$('a')。click(func);



内嵌函数



  var func = function(){
alert('inline')
};

$('a')。click(func);



匿名函数



  $('a')。click(function(){
alert('anonymous');
});

与常规函数相比,匿名函数和内联函数可能会有性能损失。请参阅 var functionName = function(){} vs function functionName(){}


The title sums up my question. An example that demonstrates the point would be nice.

解决方案

First off, there doesn't seem to be a consensus definition for inline functions in JavaScript. I consider an inline function to be a special case of a JavaScript function. An inline function is a function assigned to a variable that is created at runtime instead of at parsetime.

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() {}.

这篇关于JavaScript中的匿名和内联函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆