JQuery Iteration Functions

JQuery iterator functions like each have a syntax like this:

.each(function(index, element))

which, it would seem, implies that the function corresponding to this declaration will have to accept 2 parameters. Sort of:

function my_func(index, element){
  alert(index+":"+element);
}

For me, this gives two possible announcements:

$("li").each(my_func);

or

$("li").each(function(index, element) {alert(index+":"+element);});

The first bothers me because I do not see indexor elementtransferred to my_func. Is there any magic in jQuery that knows it my_functakes 2 parameters that it eachprovides?

Secondly, if I announced

var my_func= function(index, element){
  alert(index+":"+element);
}

it will change something. I understand that the first is declarative , and the second is expressive , but in this case they should behave the same?

, each, , :

$("li").each(function(){alert(this)});

each?

+3
1

:

$("li").each(my_func);

my_func .each(). . , my_func, . , .

my_func :

// somewhat normal way and you can easily access the index and element arguments
function my_func(index, element) {
     // code here
}

// if you don't need index and element, you don't need to declare them
// but they are still actually there and can be reached via the arguments object
function my_func() {
     // code here
     console.log(arguments[0]);   // index
}

var

:

var my_func= function(index, element){
  alert(index+":"+element);
}

function my_func(index, element){
  alert(index+":"+element);
}

. , my_func , javascript, , , , , " ", ​​ , . "", , .

my_func UNTIL, JS javascript.

, . function xxx(), , - .


, . javascript , , ( , ++), , .

javascript . , , , . javascript .

, :

 function callWithDelay(t, fn, arg1, arg2) {
     setTimeout(function() {
         fn(arg1, arg2);
     }, t);
 }

, fn.

 function myFunc(msg, color) {
     var obj = document.getElementById("error")
     obj.innerHTML = msg;
     obj.style.color = color;
 }

 callWithDelay(2000, myFunc, "Both first and last name are required", "red");

, . ( arguments, ):

 function myFunc() {
     var obj = document.getElementById("error")
     obj.innerHTML = arguments[0];
     obj.style.color = arguments[1];
 }

 callWithDelay(2000, myFunc, "Both first and last name are required", "red");

, :

 function myFunc() {
     document.getElementById("error").style.display = "none";
 }

 callWithDelay(2000, myFunc);

callWithDelay , . . callWithDelay , , , , . callWithDelay, . ( ), undefined. javascript undefined . , callWithDelay(2000, myFunc), undefined , , undefined - , myFunc . , , , undefined.


- ,

javascript , . , , , DOM:

function hide(elem) {
    elem.style.display = "none";
}

// usage
var obj = document.getElementById("test");
hide(obj);

, elem DOM , , . , , - id . , hide, , , , id.

function hide(elem) {
    // if a string was passed, find the DOM object using that string as an id
    if (typeof elem === "string") {
        elem = document.getElementById(elem);
    }
    elem.style.display = "none";
}

// first usage
var obj = document.getElementById("test1");
hide(obj);

// second uage
hide("test2");

, DOM, DOM, DOM .

function hide(elem) {
    // if a string was passed, find the DOM object using that string as an id
    if (typeof elem === "string") {
        document.getElementById(elem).style.display = "none";
    } else if (typeof elem === "object" && Object.prototype.toString.call(elem) === "[object Array]") {
        // an array was passed
        for (var i = 0; i < elem.length; i++) {
            elem[i].style.display = "none";
        }
    } else {
        // only a single DOM element was passed
        elem.style.display = "none";
    }
}

// usages
hide(obj);
hide([obj1, obj2, obj3]);
hide("test");
+5

All Articles