Your attitude is commendable I wish more programmers were like this.
These functions are called callback. They are created precisely to respond with an action to something that the environment where your application is running requires. Usually data is passed to functions. This is a way to pass algorithms to functions. Note that this function you are creating is by itself a parameter of a function you are calling. That’s how the callback (in English) works.
In this case you don’t see the calls anywhere because it’s done within the browser or at least within some library you’re using. You do not see calls because they are not the responsibility of your application.
Note that nothing prevents you from creating such a mechanism within your application. There are some advantages to doing this in some situations giving plenty of flexibility and power to the application. Of course if you create all the mechanism in your application you will have to create the call to these functions somewhere.
This is an excellent form of communication of parties who do not know each other, very used to treat events (in English), asynchronicity as used in AJAX (in English) or even to complete with some action that a module needs but must be provided by the user of that module. Often this module is a API (in English).
So you have to study the API you’re using. You have to look for documentation that explains what it does, why it is important and the various 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.
In the documentation you have the function signature. That is, it shows how the function that the API will call should be declared. Shows the 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 the 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 can be exaggeration to use something like this. In the examples I do not know if there is any real advantage in using the each()
. Basically it replaces the use of for each
to make the code smaller, as far as I know nothing more than this, at least in this case. Decreasing the code is good but is not something without cost, this should not be the main goal. It is often better to use the Vanilla JS at least because he is much faster but in this case it is also less flexible 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;
},
I put in the Github for future reference.
Source.
That’s the each()
jQuery that you don’t know where the call comes from.
Note that who does the individual operation is the function callback.call
that is part of Javascript. It is a function that serves precisely to call other functions that are passed as argument. Ultimately the function call
will be called. It has the necessary infrastructure to carry out the effective execution of the desired function.
This has nothing to do with meta-programming. At least not directly.
Documentation of getJSON()
.
Documentation of on()
.
Documentation of each()
.
Oops! Thanks! Well completed your explanation. I will read in the documentation too.
– Pedro Vinícius