javascript 我可以在 JQuery 中使用 lambda 表达式吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4964485/
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
Can I use lambda expressions in JQuery?
提问by Dave
In jQuery lambda functions, the user asks about debugging lambda expressions in JQuery. I've searched many sites, and I'm unable to find examples of lambda expressions in JQuery. Does anyone know if this is possible, and if so where can I find some examples?
在jQuery lambda 函数中,用户询问有关在 JQuery 中调试 lambda 表达式的问题。我搜索了很多网站,但无法在 JQuery 中找到 lambda 表达式的示例。有谁知道这是否可行,如果可以,我在哪里可以找到一些例子?
回答by Patrick Karcher
Lambda expression are used (among other things) as a shorthand to specifying anonymous functions(also called anonymous delegates or anonymous methods). That is, pointers to function that you define on-the-fly.
Lambda 表达式(除其他外)用作指定匿名函数(也称为匿名委托或匿名方法)的简写。也就是说,指向您即时定义的函数的指针。
See this common JQuery Ajax example:
请参阅这个常见的 JQuery Ajax 示例:
$.ajax({
url: "test.html",
context: document.body,
success: function(){
$(this).addClass("done");
} });
The successparameter uses Javascript's on-the-fly function definition and pointer. So yes, there is a kindof lambda syntax for anonymous function in javascript. In fact, this is very similar to VB.NET's lambda syntax, used very powerfully for both expression trees and anonymous functions:
在成功参数使用JavaScript对即时功能的定义和指针。 所以是的,javascript 中有一种匿名函数的 lambda 语法。事实上,这与VB.NET 的 lambda 语法非常相似,非常强大地用于表达式树和匿名函数:
Dim newNinjaList = NinjaList.Where(Function(n) n.primaryWeapon = "dagger")
So, you could say there's a lambda syntax in JQuery, though many would consider it inelegant.
因此,您可以说 JQuery 中有 lambda 语法,尽管许多人会认为它不雅。
If you mean lambda expressions to specify expression trees, then the answer is simple: no, JQuery does not use any kind of lambda syntax for expression trees.
如果您的意思是使用 lambda 表达式来指定表达式树,那么答案很简单: 不,JQuery 不对表达式树使用任何类型的 lambda 语法。
回答by jpsimons
JavaScript doesn't really have lambda expressions, because you have to explicitly return a value. Some languages like ruby automatically return the value of the last statement, but in JavaScript this doesn't work:
JavaScript 并没有真正的 lambda 表达式,因为您必须显式返回一个值。一些语言如 ruby 会自动返回最后一条语句的值,但在 JavaScript 中这不起作用:
var double = function(i) { i * 2; }
var x = double(5);
But if you add the return in there it works.
但是如果你在那里添加回报,它就可以工作。
var double = function(i) { return i * 2; }
回答by jball
The term you are looking for in JS is "anonymous function", e.g.
您在 JS 中寻找的术语是“匿名函数”,例如
$(function() {
/* in an anonymous function that is passed
to the jQuery document ready handler */
});
Specifically, the anonymous function part is the
具体来说,匿名函数部分是
function() { /* whatever */ }
回答by Art
I think they refer to a vanilla callback syntax and the specific problem they have is to do with how Visual Studio debugs JavaScript.
我认为他们指的是 vanilla 回调语法,他们的具体问题与 Visual Studio 调试 JavaScript 的方式有关。
That's what I think they are referring to as a 'lambda'
这就是我认为他们所说的“lambda”
$.get('http://...').on('data',
function(data) {
...
}
);
回答by Perpetualcoder
jQuery is an extension of the Javascript programming language. To my knowledge there is no lambda support in javascript. what you see is not really lambda expression but function chaining and passing functions as first class objects, much like Func. Lambdas are not in javascript language spec.
jQuery 是 Javascript 编程语言的扩展。据我所知,javascript 中没有 lambda 支持。你看到的并不是真正的 lambda 表达式,而是函数链接和作为第一类对象传递函数,很像 Func。Lambda 不在 javascript 语言规范中。
回答by Nathan Anderson
You might be interested in the http://jslinq.codeplex.com/project. While this does not actually bring a true lambda syntax to javascript, it allows you to use the linqextension methods (such as where, orderby, etc) on anything that is an array.
您可能对http://jslinq.codeplex.com/项目感兴趣。虽然这实际上并不带来真正的lambda语法的JavaScript,它可以让你使用linq扩展方法(例如where,orderby在任何一个数组,等等)。
回答by Sinus Mackowaty
Since the advent of ECMAScript 6 (in June 2015), you can use the "arrow operator", and thus this works:
自 ECMAScript 6 出现(2015 年 6 月)以来,您可以使用“箭头运算符”,因此可以使用:
$(()=>{
alert("Hello arrow.");
});
... at least in modern-day Chrome, Firefox, Edge and Opera, according to https://kangax.github.io/compat-table/es6/#test-arrow_functions.
...至少在现代 Chrome、Firefox、Edge 和 Opera 中,根据https://kangax.github.io/compat-table/es6/#test-arrow_functions。
Note that the arrow functionality doesn't set "this" in the called functions, so they're not very good as event handlers, compared to good old anonymous functions:
请注意,箭头功能不会在被调用的函数中设置“this”,因此与旧的匿名函数相比,它们作为事件处理程序不是很好:
<a href="#">Click me</a>
<script>
$('a[href="#"]')
// arrow function:
.click((event)=>{
event.preventDefault();
alert("arrow: "+(this==window)); // 'this' is window, not the <a>.
})
// anonymous function:
.click(function(event) {
event.preventDefault();
alert("anonymous: "+this.tagName); // 'this' is <a>.
});
</script>
回答by Aliaksandr Shpak
yah, but arrow do not work in IE11, as "let". http://kangax.github.io/compat-table/es6/
是的,但箭头在 IE11 中不起作用,如“让”。 http://kangax.github.io/compat-table/es6/
$("#CountrySelect").on("change", "#countrySelect", null, e => {
let that = $(e.target); //$(this);
if (that.val() !== "" && window.localStorage) {
localStorage.setItem("countrySelect", String(that.val()));
}
if (that.val()) {
$.ajax({
type: "POST",
url: urlControlSwitchLanguage,
data: {
language: that.val()
},
success: (data) => {
if ("error" in data) {
$("#mainDataBodyTable").empty();
$("#mainDataBodyTable").append(`<div>${data.error}</div>`);
console.error(data.error);
} else {
$("#mainDataBodyTable").empty();
countTableElement = createTable$(data, countTableElement);
$("#linkDownloads").attr("href", urlControlGetFile + "?language=" + that.val());
}
},
error: (xhr, ajaxOptions, thrownError) => {
console.log(xhr);
console.log(ajaxOptions);
console.log(thrownError);
}
});
}
回答by Rohit Dodiya
Use this code:
使用此代码:
lstResource.find(x => x.Module == Module && x.Form == Form && x.ResourceName == Resource).Value
Except for IE, it will work for every place.
除了 IE,它适用于任何地方。

