Can I use lambda expressions in jQuery?

9 answers

A lambda expression is used (among other things) as an abbreviation for anonymous functions (also called anonymous delegates or anonymous methods). That is, pointers to a function that you define on the fly.

See this general jQuery Ajax example:

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $(this).addClass("done");
  } });

success Javascript . , , - javascript. , VB.NET, :

Dim newNinjaList = NinjaList.Where(Function(n) n.primaryWeapon = "dagger")

, , JQuery , .

- , : , JQuery - .

+6

JavaScript -, . , ruby, , JavaScript :

var double = function(i) { i * 2; }
var x = double(5);

, .

var double = function(i) { return i * 2; }
+3

, JS, - " ", .

$(function() { 
    /* in an anonymous function that is passed 
       to the jQuery document ready handler */ 
 });

, -

function() { /* whatever */ } 
+1

jQuery - Javascript. , javascript . , , , , Func. Lambdas javascript.

0

, , , , , Visual Studio JavaScript.

, , ""

$.get('http://...').on('data', 
    function(data) {
        ...
    }
);
0

http://jslinq.codeplex.com/. javascript, linq (, where, orderby ..) , .

0

ECMAScript 6 ( 2015 ) " ", :

$(()=>{
  alert("Hello arrow.");
});

... Chrome, Firefox, Edge Opera, https://kangax.imtqy.com/compat-table/es6/#test-arrow_functions.

Note that the arrow functionality does not set “this” in the called functions, therefore they are not very good as event handlers compared to the 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>
0
source

yes, but the arrow does not work in IE11, since "let". http://kangax.imtqy.com/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);
        }
    });
}
0
source

Use this code:

lstResource.find(x => x.Module == Module && x.Form == Form && x.ResourceName == Resource).Value

With the exception of IE, it will work for all places.

0
source

Source: https://habr.com/ru/post/1791370/


All Articles