Reputation: 71
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?
Upvotes: 7
Views: 22569
Reputation: 271
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.
Upvotes: 1
Reputation: 392
yah, but arrow do not work in IE11, as "let". 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);
}
});
}
Upvotes: 0
Reputation: 548
Since the advent of ECMAScript 6 (in June 2015), you can use the "arrow operator", and thus this works:
$(()=>{
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.
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:
<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>
Upvotes: 1
Reputation: 23613
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.
See this common JQuery Ajax example:
$.ajax({
url: "test.html",
context: document.body,
success: function(){
$(this).addClass("done");
} });
The success parameter 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:
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.
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.
Upvotes: 7
Reputation: 6878
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 linq
extension methods (such as where
, orderby
, etc) on anything that is an array.
Upvotes: 0
Reputation: 28110
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:
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; }
Upvotes: 3
Reputation: 24597
I think they refer to a vanilla callback syntax and the specific problem they have is to do with how Visual Studio debugs JavaScript.
That's what I think they are referring to as a 'lambda'
$.get('http://...').on('data',
function(data) {
...
}
);
Upvotes: 0
Reputation: 25014
The term you are looking for in JS is "anonymous function", e.g.
$(function() {
/* in an anonymous function that is passed
to the jQuery document ready handler */
});
Specifically, the anonymous function part is the
function() { /* whatever */ }
Upvotes: 1
Reputation: 13591
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.
Upvotes: 0