Converting a javascript library to chain methods
<pre><code>var Stuff = (function() {
    return {
        getId: function (id) {
            return document.getElementById(id);
        },
        attr: function (ele, attr, newVal) {
            var newVal = newVal || null;
            if (newVal) {
                ele.setAttribute(attr, newVal);
            } else {
                var attrs = ele.attributes,
                attrslen = attrs.length,
                result = ele.getAttribute(attr) || ele[attr] || null;

                if (!result) {
                    for (var i = 0; i &lt; attrslen; i++)
                        if (attr[i].nodeName === attr) result = attr[i].nodeValue;
                }
                return result;
            }
        }
    }
})();
</code></pre>

<p>With this html:</p>

<pre><code>&lt;div id="foo" data-stuff="XYZ"&gt;Test Div&lt;/div&gt;
</code></pre>

<p>The current implementation:</p>

<pre><code>(function ($) {
    console.log(
        $.attr($.getId('foo'), 'data-stuff') // XYZ
    );
})(Stuff);
</code></pre>

<p><strong>How do I rewrite my library above to make it chain like the following?</strong></p>

<pre><code>(function ($) {
    console.log(
        $.getId('foo').attr('data-stuff') // XYZ
    );  
})(Stuff);
</code></pre>
