Background
jQuery specializes in dealing with elements of DOM. When you do something like:
var x = $("seletor")
It creates an object that contains - among other things - a list of zero or more elements. Other ways to create this object:
var a = document.getElementById("a");
var b = document.getElementById("b");
var c = document.getElementById("c");
var x = $([a, b, c]); // Lista com 3 elementos
var x = $(a); // Lista com 1 elemento
var x = $(); // Lista com 0 elemento
var x = $("<div><span>Teste</span></div>"); // Cria um elemento e põe na lista
var x = $("<p>A</p><p>B</p><p>C</p>"); // Idem, para mais de um elemento
All functions of the "jQuery object" then operate on lists of FOD elements. Other user-defined functions can be created, but they are expected to also act on DOM element lists:
$.fn.minhaFuncao = function() {
// Aqui "this" é um objeto jQuery
// Pode-se fazer isso:
return this[0].value; // Retorna o valor do primeiro elemento
// Ou isso:
return this.each(function() {
this.value = 10;
}); // Faz todos elementos terem o valor 10, e retorna o próprio objeto (pra chaining)
};
Answer
Using jQuery in arbitrary object lists is complicated, as it would mix incompatible data types. If you wanted to deal with date lists (or worse: isolated dates) all original jQuery functions could be called on those dates (causing errors as they are different types) and similarly the new functions you create could also be called in the DOM element lists (idem).
Mix the two, so it’s not a good idea... I don’t know a generalized solution to do this for "type A lists," but there are libraries like Underscore.js using a concept similar to that of jQuery (do wrap on an object to introduce its own functions, including with chaining) to accomplish its specific purposes.
If the question is adding methods to individual elements of a kind, just touch your prototype:
Date.prototype.formatarData = function() {
// "this" aqui é um objeto Date
}
var agora = new Date();
agora.formatarData(); // Chama seu método num objeto com a data atual
Some consider this bad practice (myself, for example) by "polluting the namespace", but used as moderation and care can be a good option...
Can you explain better what you intend to do?
– Sergio
I think you can solve your problem differently. It’s not quite there... I mean: what you intend to do can probably be achieved without the need to "create a plugin for the service call function", as you say.
– J. Bruni
the string that went in the call of jQuery would be a url and the plugin also creates functions to mess with a cloud storage service that I created.
– Iago Bruno
At least I thought it would return the initial string but in some tests did not work very well, the value always back Undefined
– Iago Bruno
@Iagobruno It turns out that jQuery plays
$(string)
like using a selector, it has no way of differentiating a string whose content is a string date whose content is a selector (ok, in this case it could, but because the syntaxes are sufficiently distinct). That would be a case of DWIM, which in my opinion is a bad practice [in this context at least]. An example are the spreadsheets, which deal with1
as a number,1/2
as a fraction and1/2/3
how to date! Types in a programming language have to be unmistakable.– mgibsonbr
no one understood that the date format was just an example =p. But @mgibsonbr but if I were to do it I would try to use the jQuery context, for example: $('12/08/2013', 'date'). getDay(). But thank you for the reply/comment.
– Iago Bruno