note: I'm JavaScript beginner, so excuse me if this question is too naive.
I'm customizing this jquery alternative small original-library
https://github.com/kylebarrow/chibi/blob/master/chibi.js[
^] which works the way I want and returns a nodes array(not array-like-object) which can be used with native javascript methods like forEach, slice etc.
But I want to slim it down and optimized only for modern browsers using prototype closure (actually I don't know much about prototype closure, but I want to use it because I heard it saves memory).
Please help me. I hope your help can get rid of my headache & stress I'm having from last 3 days. I asked this question on StackOverflow which went unanswered.
failed to build simple JQuery alternative with prototype closure and returning array to work with native JavaScript methods - Stack Overflow[
^]
What I have tried:
(function() {
'use strict';
var d = document,
w = window;
function nodeLoop(fn, nodes) {
for (var i = 0, len = nodes.length; i < len; i++) {
fn(nodes[i], i, nodes);
}
}
function chibi(selector) {
var cb, nodes = [],
json = false,
nodelist, i, len;
if (selector) {
if (selector.nodeType && selector.nodeType === 1) {
nodes = [selector];
} else if (typeof selector === 'object') {
json = (typeof selector.length !== 'number');
nodes = selector;
} else if (typeof selector === 'string') {
nodelist = d.querySelectorAll(selector);
len = nodelist.length;
for (i = 0; i < len; i += 1) {
nodes[i] = nodelist[i];
}
}
}
console.log('Selected nodes:' + nodes);
}
function cb(selector) {
return new chibi(selector);
}
chibi.fn = chibi.prototype = {
on: function(event, fn) {
console.log(this + this.length);
nodeLoop(function(elm) {
console.log(elm);
elm.addEventListener(event, fn, false);
}, this);
return this;
},
html: function(value) {
if (value || value == '') {
nodeLoop(function(elm) {
elm.innerHTML = value;
}, this);
return this;
}
if (this[0]) {
return this[0].innerHTML;
}
}
};
window.$ = cb;
}());
$('.heading').on('mouseover', function(event) {
$(this).html('replacing heading text');
$($('ul li').slice(2)).forEach(function(elm, index) {
elm.innerHTML = index;
});
});
<html>
<body>
<h1 class="heading">This is demo Heading</h1>
<ul>
<li>first li</li>
<li>second li</li>
<li>third li</li>
</ul>
</body>
</html>