Gugugu, a minimalist implementation of jquery
Source code analysis
var jQuery = window.jQuery = function(selector) {
return new jQuery.prototype.init(selector);
}
// Map the jQuery namespace to the '$' one
window.$ = jQuery;
JQuery. FN = jQuery. Prototype = {init: function (selector) {var nodes = document. Queryselectorall (selector);
for (VaR I in nodes) {this [i] = nodes [i];
} return this;
}
/ / omit 100000 words
}}
jQuery.prototype.init.prototype = jQuery.prototype;
Complete implementation
var jQuery= function (selector) {
return new jQuery.fn.init(selector);
}
jQuery.fn = jQuery.prototype = {
init: function (selector) {
var nodes = document.querySelectorAll(selector);
for (var i in nodes) {
this[i] = nodes[i];
}
return this;
},
element: function (callback) {
for (var i = 0; i < this.length; i++) {
callback(this[i]);
}
},
text: function (content) {
if (content == '' || content) {
this.element(function (node) {
node.innerHTML = content;
})
return content;
} else {
return this[0].innerHTML;
}
},
}
jQuery.prototype.init.prototype = jQuery.prototype;
window.$ = jQuery;
end