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/:
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
Post a Comment