Anyone can create a library, and this is very useful in small projects where you need specific methods and so often it is better to organize the code.
Basically there are two approaches. Create functions that accept all data as arguments, or extending the prototype of elements of the DOM or guys javascript.
jQuery approached by the first option, Mootools for example by the second.
In any case it is important to bear in mind that there may be clashes with other libraries and so plan well what names are used.
If you want an API around an object, yours, you could do it like this:
var MetodosMeus = {
foo: function(str){
// fazer algo
},
bar: function(arr){
// fazer algo
}
};
And then you call for example:
var strLimpa = MetodosMeus.limpar(stringSuja);
Doing the same via extension in the prototype would be
String.prototype.limpar = function(){
// fazer algo
};
and to use would:
var strLimpa = stringSuja.limpar();
I mentioned above Mootools which is also a modular library, which jQuery does not allow in the same way. So if your library gets big, it’s best to have modules that can work as additions. That way you can only use what you need and keep the code and files light.
Thank you very much, I’m going to study right now.
– Lucas Lima Muller
Just tell me one thing, in case I want to do a function whose variables of the method to be executed have a default value, but which can be changed if I want to do it, I would do it with parameters in the same immediate function? An example of what I’m talking about is Jquery’s fadein(), which has a default value for duration, but can be customized.
– Lucas Lima Muller
@Lucas, I think this is another question. But jQuery treats this internally, within the function
– Wallace Maxters
Do not mark the answer as solved yet. I believe better answers will come up
– Wallace Maxters
Okay, I’ll wait
– Lucas Lima Muller