I've been going through the jQuery code and I have seen a few links that have this question. I am asking it again because I did not really understand any of them so please do not mark this question as duplicate. I've gone through the following stackoverflow links:
How can jQuery return an array and still have it to be a jQuery object
How does jQuery return an array of the selected objects
Gone through all of those links and I do not get how it works. All I ask is a very simple example of returning an array from a constructor object and still chaining it. I have tried the following code:
(function(window, document, undefined)
{
var lhd = function(selector)
{
return new Lhd(selector);
}
function Lhd(selector)
{
var elem = document.querySelectorAll(selector),
elems = {};
return this.makeArray(elems, elem);
}
Lhd.prototype = {
makeArray: function(arr, result)
{
var ret = result || [];
Array.prototype.push.apply(ret, arr);
return ret;
},
click: function()
{
console.log('click');
return this;
}
};
window.lhd = lhd;
})(window, document);
I'm able to return an array but unable to chain it.