I realized using jQuery for my current project is a bit over-kill as I only used ~10 methods (at most) so I decided to create a much smaller library with the API-style of jQuery.
The function for the library (called Lib), returns 3 properties:
results (contains the results of the query)
selector (contains the selector used to get the results)
length (the length of the results)
The function for the Library looks like this:
function Lib(selector, context) {
context = context || window.document;
if(window === this) {
return new Lib(selector, context);
}
this.results = map(context.querySelectorAll(selector));
this.selector = selector;
this.length = results.length;
return this;
}
Map function:
function map(collection) {
var results = [];
for(var i = 0; i < collection.length; i++) {
results.push(collection[i]);
}
return results;
}
Every time I access the "this" object from a method in Lib.prototype I have to use this.results to modify the elements this.selector to get the selector and this.length to get the length.
Is it possible to pollute the "this" object with both elements and properties.
E.G: using the "this" object to access the elements,
the "this.selector" property to access the selector and
the "this.length" to access the length of the elements?
Also how do I implement it?
For those of you wondering here is the (COMPLETE) list of Methods:
Lib("div").html("<div id=\"div-one\">Some Awesome HTML</div>")
Lib("div").text("Some Awesome Text")
Lib("div").attr("placeholder", "Search")
Lib("div").attr("placeholder")
Lib("div").css("background-color", "#fff")
Lib("div").css("background-color")
Lib("div").on("type", function(){/* do something when div has been clicked */})
Lib("div").off("type", function(){/* do something when div has been clicked */})
Lib("div").each(function(index, value){/* loop through each div and do something with them */})
Lib("input").blur(function(){/* do something when the focus has left input */})
Lib("input").focus(function(){/* do something when input has been focused on */})
NOTE: please don't bother with support for IE<9 (IE 6-9 needs to die)