I have just seen a google tech talk presented by John Resig where he said jQuery operates as an array. Following that advice I have been playing with a subclassed array and it works great but I have been looking through the jQuery source and can't see that they have used the same method of
jQuery.prototype = new Array();
And I can't see it even taking the native Array.prototype.methods with call/apply or in the prototype chain in the window.$ object, so I am wondering how does the jQuery object return an array of the selected elements.
I have tried using an ordinary object, but if returning an array it stops the ability to chain commands
If it is possible to take some methods from Array.prototype what is essential to return an array?
This is what I was playing with.
;(function(window, document, undefined){
function MyLib(){};
// prototype constructor functions
function Core(){};
function Methods(){};
// create new instance of the MyLib object and call the construct method
function myLib(selector){
return new MyLib().construct(selector);
}
// allow adding new methods to the prototype from the window.
// eg $.extend('module', 'name', 'function')
myLib.extend = function(module, name, fn){
if(typeof fn === 'function'){
if(!MyLib.prototype[module][name]){
MyLib.prototype[module][name] = fn;
}
} else if(typeof fn === 'object'){
for(var key in fn){
if(!MyLib.prototype[module][key]){
MyLib.prototype[module][key] = fn[key];
}
}
} else {
throw new Error("invalid type, function or objects are required");
}
}
MyLib.prototype = new Array();
MyLib.prototype.core = new Core();
MyLib.prototype.methods = new Methods();
MyLib.prototype.construct = function(selector){
var elems = document.getElementsByTagName(selector);
Array.prototype.push.apply(this, Array.prototype.slice.call(elems, 0, elems.length));
return this;
};
// access to prototype objects with $.core && $.methods
myLib.core = MyLib.prototype.core;
myLib.methods = MyLib.prototype.methods;
// give the window access to the constructor function
window.$ = myLib;
// give the window access to the prototype object for debugging
window.$$ = MyLib;
})(window, document);
// this adds a new method to the methods object within the prototype
$.extend('methods', 'test', function(){alert('method successfully added')});
// the added method can be accessed with
$.methods.test();
// or
$('tagName').test();
thanks for any answers