1) How does parameter 1 (index) know as an integer?
jQuery passes an integer.
2) How does a function know to increase the index?
The callback does not increment index , the jQuery method does.
3) How is the second parameter (oldValue) known for storing the old property value (before modification)?
jQuery passes it.
Answers to questions 1-3 are perhaps best understood by a function that does something similar to $.attr :
Array.prototype.each = function (f) { var i; for (i=0; i < this.length; ++i) { f(i, this[i]); } }; ['zero', 'one', 'two'].each(function (i,item) {console.log({i: item})});
f is a callback. each is responsible for iterating over the collection and calling f for each index and element. The same code structure can be used for functions:
/* Map each item in a sequence to something else, * returning a new sequence of the new values. */ Array.prototype.map = function (f) { var i, result = []; for (i=0; i < this.length; ++i) { result[i] = f(i, this[i]); } return result; }; ['zero', 'one', 'two'].map(function(i,item) {return item.length}); // result: [4, 3, 3] /* Return a sequence of the items from this sequence * for which 'keep' returns true. */ Array.prototype.filter = function (keep) { var i, result = []; for (i=0; i < this.length; ++i) { if (keep(i, this[i])) { result.push(this[i]); } } return result; }; ['zero', 'one', 'two'].filter(function(i,item) {return item.length <= 3}); // result: ['one', 'two']
Running mapconcat , foldl and foldr remains as an exercise. As another exercise, rewrite map and filter in terms of each .
Note. These functions are intended only to illustrate how callbacks work. They can cause production code problems.
4) Is this a jQuery construct? What is JSON? That's cool. it works, but ... what is this “callback value” made from?
Callbacks are a common method that jQuery makes extensive use of. They are a key feature of functional programming , where functions are data that can be used in the same way as other types of data. That way, you have functions that take functions as arguments and can return functions. In certain contexts, callbacks are also known as “continuations” and form the basis of the Continuation Continuation Style ( CPS ). This is especially important for asynchronous function calls [ 2 ] (where the function returns before completion of the calculations, as against synchronous calls ), for example, it is used for Ajax requests. To see some of the features of CPS, read Use Continuation to Develop Complex Web Applications .
Another aspect of this is the “value” in the “callback”, since JS is a dynamically typed language (types are associated with data, not variables), formal parameters can be bound to objects of any type. Then the function can behave differently depending on what is being transmitted. This is sometimes accomplished by examining the type of argument that ad-hoc polymorphism operates (a function, not a language, must be manually submitted). However, parametric polymorphism or (otherwise) duck typing should always be preferable to studying argument types. Parametric polymorphism is achieved due to the fact that all types that can be passed to this function support the same interface (method names, arguments, preconditions, postconditions, etc.). For example, all types of sequences must have the length property and be indexed with integers; as long as this is done, you can use your own sequence type with many functions that take arrays.
I'm not sure what you mean by JSON, but that is probably not what is usually meant. JSON is a data exchange format based on a limited version of the JS object literal syntax. JSON is not involved in any example code or quoted text.