What is the “i” in the “function (i)” in the following JavaScript?

The following code has "function (i)", but "i" has not been declared anywhere before this statement.

ul.css({width: 10, overflow: 'visible'}).retarder(100, function(i){ i.css('visibility', 'visible').animate( {width: ul[0].wid,left:-50}, {duration: 500, complete : function(){ ul.css('overflow', 'visible'); }} ); }); 

It looks like it might look like a C ++ "this" statement. It is right?

+7
javascript jquery
source share
8 answers

i is just a function parameter that is passed in by the retarder function of retarder anonymous function.

What does he do:

 ul.css({width: 10, overflow: 'visible'}).retarder(100, callback_function); 

and the callback is defined through an anonymous function:

 function(i) { ... } 

So, i is a parameter definition of an anonymous function.

+4
source share

Looks like a function declaration:

 function(i) { // ..... } 

So i is the value passed to the function (which is declared built-in as an anonymous function) as its first parameter, presumably by the internal operations of the retarder method through which you pass the function to.

Rewriting the code to make it more readable makes it easier to understand:

 ul.css( { width: 10, overflow: 'visible' } ).retarder(100, function(i) { i.css('visibility', 'visible').animate( { width: ul[0].wid, left:-50 }, { duration: 500, complete: function() { ul.css('overflow', 'visible'); } } ); } ); 

And you can rewrite it even more clearly:

 ul.css( { width: 10, overflow: 'visible' } ).retarder(100, functionToPassToRedtarder); function functionToPassToRetarder(i) { i.css('visibility', 'visible').animate( { width: ul[0].wid, left:-50 }, { duration: 500, complete: functionToPassToComplete } ); } function functionToPassToComplete() { ul.css('overflow', 'visible'); } 
+11
source share

This is a function parameter.

+7
source share

it creates an anonymous function that takes one argument, which should then be denoted as i in the function.

+5
source share

The reason you might not understand this is because you are not familiar with using anonymous functions in JavaScript. Most likely, you are familiar with something like:

 function double(i){ return i + i; } 

i is a parameter of the double function. In JavaScript, this same function can be performed as follows:

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

In this case, an anonymous function is created and then assigned to the double variable. i is still just a parameter. Then they can be called as double(3) .

In the above example, instead of assigning an anonymous function to a variable, it was passed as an argument to another function.

+2
source share

The variable i in the declaration function of an anonymous function (i) is the name used for the first parameter inside the function body. It does not match any variable elsewhere on your page.

+1
source share

If you are trying to decode the obfuscation code that I think you are, then you are probably looking for a definition of this function ...

 $.fn.retarder = function(delay, method){ var node = this; if (node.length){ if (node[0]._timer_) clearTimeout(node[0]._timer_); node[0]._timer_ = setTimeout(function(){ method(node); }, delay); } return this; }; 

I needed to go a little deeper to find it, because it was dynamically generated in eval ().

So, to answer your question, the parameter "i" is an object of "ul" (in the code that you published).

If you look at the moderator function, it returns "this", like most other jquery plugins, and therefore supports the plug-in chaining ability.

isn't de scary fun?

+1
source share

The variable i represents the actual object (for example, the this )


Explain more:

1) Is the parameter an anonymous function, as we see:

 .retarder(100, function(i){...}) 

2) Is a reference to the current object ( this ).

When this jquery plugin finishes, it calls the callback function using:

 (callbackFunction)(this) 

Where is the parameter.

0
source share

All Articles