jQuery JavaScript lambda 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17139445/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:30:33  来源:igfitidea点击:

JavaScript lambda functions

javascriptjquery

提问by svidgen

I was looking to have a good example for using lambda functions or anonymous functions within JavaScript with no luck.

我希望有一个很好的例子来在 JavaScript 中使用 lambda 函数或匿名函数,但没有运气。

Does jQuery have built-in functions to implement lambda too?

jQuery 是否也有内置函数来实现 lambda?

回答by Paul S.

In JavaScriptthese are called function expressions(using functionas an operator as opposed to declarations, using functionas a statement), and can be named or anonymous.

JavaScript 中,它们被称为函数表达式function用作运算符而不是声明function用作语句),并且可以命名或匿名。

It's really as simple as doing something that tells the compiler it is an expression (e.g. var x =) then writing a function normally, and adding a delimiter ;on the end.

它真的很简单,就像做一些告诉编译器它是一个表达式(例如var x =)然后正常编写一个函数,并;在最后添加一个分隔符一样简单。

function invoke(lam) {
    console.log(
        lam()
    );
}

var lambda = function () {return 'foo';};

invoke(lambda); // "foo" logged

As with any functionin JavaScript, the scope is inherited from where it is defined, not where it is invoked.

正如任何功能的JavaScript,范围从它被定义为其中继承,而不是在那里它被调用。

Self-invoking and anonymous functions are nearly always function expressions. For self-invoking functions, the (before functionmeans the code is interpreted as an expression, then the (preferred) ());or (alternate) )();invokes it immediately.

自调用函数和匿名函数几乎总是函数表达式。对于自调用函数,(beforefunction表示代码被解释为表达式,然后 (preferred)());或 (alternate))();立即调用它。

You may need to remember that a function expressionby itself is not hoisted. If you need hoisting for it to avoid a Reference Error, combine with var. The function itself will still not be fully available until the code passes the line where it is defined.

您可能需要记住,函数表达式本身不会被提升。如果您需要提升它以避免引用错误,请与var. 在代码通过定义它的行之前,函数本身仍然不会完全可用。

For a named function expressionthe name is only available insidethe function and not outside (some old versions of IE leaked the name though). To describe this, I'll use two examples, one self invoking and one vard;

对于命名函数表达式,名称仅函数内部可用,而在外部不可用(尽管某些旧版本的 IE 泄漏了名称)。为了描述这一点,我将使用两个示例,一个自调用和一个vard;

// self-invoked
(function foo() {
    console.log('inside:', foo); // foo is defined here
}());
console.log('outside:', foo); // ReferenceError: foo is not defined

// var
var bar = function foobar() {
    console.log('inside:', foobar); // foobar is defined here
    console.log('inside:', bar); // bar is defined here too (=== foobar)
};
bar(); // invoke
console.log('outside:', bar); // bar is also defined here, but..
console.log('outside:', foobar); // ReferenceError: foobar is not defined

回答by svidgen

A lambda function (anonymous function) is really just a function declaration without a name (it can be assigned to a variable later and still technically be a lambda). One common example is a self-executing function:

一个 lambda 函数(匿名函数)实际上只是一个没有名称的函数声明(它可以在以后分配给一个变量,并且在技术上仍然是一个 lambda)。一个常见的例子是一个自执行函数:

(function(){
  /*
    do stuff
  */
})();

Another common example is passing a function as a parameter for an AJAX or JSONP callback, a timeout, or a sort():

另一个常见示例是将函数作为参数传递给 AJAX 或 JSONP 回调、超时或sort()

setTimeout(
    function() {
        console.log('lambda!');
    },
    100
);

On the "receiving" end of a lambda, you invoke the function parameter as you would any other function:

在 lambda 的“接收”端,您可以像调用任何其他函数一样调用函数参数:

functionThatUsesLamba(function(s) { console.log(s); });

function functionThatUsesLambda(logFn) {
  if (typeof(logFn) == 'function') {
    logFn('lambda');
  } else {
    throw "logFn must be a function!!!";
  }
}