Created
December 23, 2011 03:58
-
-
Save cowboy/1513080 to your computer and use it in GitHub Desktop.
Why doesn't jQuery "do it kinda like this" ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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