Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created December 23, 2011 03:58
Show Gist options
  • Select an option

  • Save cowboy/1513080 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/1513080 to your computer and use it in GitHub Desktop.
Why doesn't jQuery "do it kinda like this" ?
function xQuery(selector, context) {
if (xQuery.caller !== xQuery) {
return new xQuery(selector, context);
}
if (selector === "body" && !context && document.body) {
this.context = document;
this[0] = document.body;
this.selector = selector;
this.length = 1;
}
// etc
};
xQuery.fn = xQuery.prototype = {
constructor: xQuery,
selector: "",
xquery: "1.0.0",
length: 0,
splice: [].splice
// etc
};
xQuery.fn.method = function() {
return this;
};
// in chrome console
jQuery('body') instanceof jQuery // true
console.log(jQuery('body')); // logs like an array
console.dir(jQuery('body')); // logs object log jQuery.fn.jQuery.init[1]
xQuery('body') instanceof xQuery // true
console.log(xQuery('body')); // logs like an array
console.dir(xQuery('body')); // logs object like xQuery[1] (easier to understand?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment