Explanation for index and value inputs to anonymous function in javascript -


i have question on how anonymous functions work in javascript. saw beautiful piece of code toggle "disabled" html element [from post link stack overflow ]:

$('#el').prop('disabled', function(i, v) { return !v; }); 

the inputs anonymous function , v index , value.

why that? because of .prop() or somehow property of anonymous functions? there other inputs available? thanks

solution: answer question in docs .prop() api.jquery.com/prop/:

enter image description here

it's how jquery .prop() method implemented. if sees second parameter function, calls function each matched element, passing element index , attribute value.

inventing api in javascript involves making decisions situations involving callback api clients use. there no hard-and-fast rules in general, though contexts have conventions should followed when possible. example node world, it's common callbacks passed 2 arguments, (possibly null) error , data relevant operation.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -