Click here to Skip to main content
15,071,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
jQuery seems to be eluding me. I don't understand how it works. I'm trying to replicate a small version of the library(for educational purposes) and it isn't working at all. My function returns an array but is unable to call any of the prototype functions. I ask simply for a small piece of code that shows how jQuery returns an array and chains to its prototype. The code I have:

JavaScript
(function(window, undefined)
    {
        var Duplicate = function(selector)
        {
            return new Duplicate.fn.init(selector);
        };

        Duplicate.fn = Duplicate.prototype = {
            click: function()
            {
                console.log('click');

                return this;
            }
        };

        var init = Duplicate.fn.init = function(selector)
        {
            var ret = [],
                selector = document.querySelectorAll(selector);

            if( selector.length )
            {
                for( var i=0; i<selector.length; i++ )
                {
                    ret.push(selector[i]);
                }
            }

            return ret;
        };

        init.prototype = Duplicate.fn;

        window.duplicate = Duplicate;
        
        duplicate('#mydiv'); // returns [ <div id="mydiv"></div> ] - working perfect
        // but
        duplicate('#mydiv').click();   // returns no such function
    })(window);


How do I chain
JavaScript
ret
to the prototype?
Posted
Comments
   
Sorry, it all looks gibberish to me. Your "duplicate" is not a function. Also, you names parameters "window" and "undefined" hiding names of two already defined objects. What is "fn"? "init"? And so on...
And array is an object...
What did you try to achieve?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900