Your attitude is commendable would like more programmers to be like this.
These functions are called callback . They are created just to respond with an action to something that the environment where your application is running requires. Data is normally passed to functions. This is a way to pass algorithms to functions. Note that this function you are creating is itself a parameter of a function you are calling. This is how the callback ( in English ) works.
In this case you do not see the calls anywhere because this is done inside the browser or at least inside some library that you are using. You do not see calls because they are not your application's responsibility.
Note that nothing prevents you from creating such a mechanism within your application. There are some advantages to doing this in some situations giving enough flexibility and power to the application. Of course if you create the entire mechanism in your application you will have to create the call to these functions somewhere.
This is an excellent way of communicating parts that are not known, much used to treat in English ), asynchronicity as used in # ( API ( in English ).
So you have to study the API you're using. You have to look for the documentation that explains what it does because it is important and the different ways to use it. Knowing all the information you can be much more creative. This is what sets true developers apart from cake recipe followers.
The documentation has the function signature. That is, there it shows how to declare the function that the API will call. It shows what parameters it should receive and what it should return. Usually describes it gives examples of the minimum that should be done in the body of the function. You can do what and whatever way you want internally, you just need to respect the input and output protocols and do something minimally useful that in some cases may even be nothing.
In some cases it may be exaggerated to use something like this. In the examples I'm using I can not tell if there is any real advantage in using each
. In the background it replaces the use of for each
to leave the code smaller, as far as I know nothing more than this, at least in this case. Decrease code is fine but is not something without cost , this should not be the main objective. It is often best to use Vanilla JS at least because it is much faster but in this case it is less flexible as well and legibility is questionable.
But this is an easy case to understand:
// args is for internal usage only
each: function( obj, callback, args ) { //note que args não faz parte da API pública
var i = 0,
length = obj.length,
isArray = isArraylike( obj );
if ( args ) { //aqui trata quando há algo interno acontecendo
if ( isArray ) {
for ( ; i < length; i++ ) {
if ( callback.apply( obj[ i ], args ) === false ) {
break;
}
}
} else {
for ( i in obj ) {
if ( callback.apply( obj[ i ], args ) === false ) {
break;
}
}
}
// A special, fast, case for the most common use of each
} else { //tratamento do caso público que é que nos interessa
if ( isArray ) { //tratamento especial se for um array
for ( ; i < length; i++ ) { //varrrrá todo array passado
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break; //encerra quando a chama acima falhar
}
}
} else { //outros objetos
for ( i in obj ) { //vai analisar cada elemento do objeto passado
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
break;
}
}
}
}
return obj;
},
Font .
This is each
of jQuery that you do not know where the call comes from.
Note that whoever does the individual operation is the callback.call
function that is part of JavaScript. It is a function that serves just to call other functions that are passed as an argument. Ultimately the call
function will be called. It has the necessary infrastructure to perform the actual execution of the desired function.
This has nothing to do with meta-programming. At least not directly.
getJSON documentation.
documentation on .
each documentation.