Last active
May 16, 2017 16:11
-
-
Save justinbmeyer/690253663b39f79bd867c2aac8f5db52 to your computer and use it in GitHub Desktop.
starting-my-jquery.js
This file contains 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() { | |
$ = function(selector) { | |
if( !(this instanceof $) ) { | |
return new $(selector); | |
} | |
var elements = document.querySelectorAll(selector); | |
this.length = elements.length; | |
for(var i = 0; i< elements.length; i++) { | |
this[i] = elements[i]; | |
} | |
}; | |
$.extend = function(target, object) { | |
for(var prop in object) { | |
target[prop] = object[prop]; | |
} | |
return target; | |
}; | |
// Static methods | |
var isArrayLike = function(obj) { | |
if(typeof obj.length === "number") { | |
if(obj.length === 0) { | |
return true; | |
} else if(obj.length > 0 ) { | |
return (obj.length - 1) in obj; | |
} else { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
}; | |
$.extend($, { | |
isArray: function(obj) { | |
return Object.prototype.toString.call(obj) === "[object Array]"; | |
}, | |
each: function(collection, cb, context) { | |
if( isArrayLike(collection) ) { | |
for(var i = 0 ; i < collection.length; i++) { | |
if( cb.call(context, i, collection[i], collection) === false ) { | |
break; | |
} | |
} | |
} else { | |
for(var prop in collection) { | |
if( cb.call(context,prop, collection[prop], collection) === false ) { | |
break; | |
} | |
} | |
} | |
return collection; | |
}, | |
makeArray: function(arr) { | |
var array = []; | |
$.each(arr, function(index, value){ | |
array.push(value); | |
}); | |
return array; | |
}, | |
proxy: function(fn, context) { | |
return function(){ | |
return fn.apply(context, arguments); | |
}; | |
} | |
}); | |
$.each({ | |
html: "innerHTML", | |
val: "value" | |
}, function(method, property){ | |
$.prototype[method] = function(newValue){ | |
if(arguments.length) { | |
$.each(this, function(i, element){ | |
element[property] = newValue; | |
}); | |
return this; | |
} else { | |
return this[0][property]; | |
} | |
}; | |
}); | |
var getText = function(node){ | |
var text = ""; | |
$.each(node.childNodes, function(i, child){ | |
if(child.nodeType === Node.ELEMENT_NODE) { | |
text = text + getText(child); | |
} else if(child.nodeType === Node.TEXT_NODE){ | |
text = text + child.nodeValue; | |
} | |
}); | |
return text; | |
}; | |
$.extend($.prototype, { | |
text: function(newText) { | |
if(arguments.length) { | |
return $.each(this.html(""), function(i, element){ | |
var textNode = document.createTextNode(newText); | |
element.appendChild( textNode ); | |
}); | |
} else { | |
return getText(this[0]); | |
} | |
}, | |
find: function(selector) {}, | |
next: function() {}, | |
prev: function() {}, | |
parent: function() {}, | |
children: function() {}, | |
attr: function(attrName, value) {}, | |
css: function(cssPropName, value) {}, | |
width: function() {}, | |
offset: function() { | |
var offset = this[0].getBoundingClientRect(); | |
return { | |
top: offset.top + window.pageYOffset, | |
left: offset.left + window.pageXOffset | |
}; | |
}, | |
hide: function() {}, | |
show: function() {}, | |
// Events | |
bind: function(eventName, handler) {}, | |
unbind: function(eventName, handler) {}, | |
has: function(selector) { | |
var elements = []; | |
$.each(this, function(i, el) { | |
if(el.matches(selector)) { | |
elements.push(el); | |
} | |
}); | |
return $( elements ); | |
}, | |
on: function(eventType, selector, handler) { | |
return this.bind(eventType, function(ev){ | |
var cur = ev.target; | |
do { | |
if ($([ cur ]).has(selector).length) { | |
handler.call(cur, ev); | |
} | |
cur = cur.parentNode; | |
} while (cur && cur !== ev.currentTarget); | |
}); | |
}, | |
off: function(eventType, selector, handler) {}, | |
data: function(propName, data) {}, | |
// Extra | |
addClass: function(className) {}, | |
removeClass: function(className) {}, | |
append: function(element) {} | |
}); | |
$.buildFragment = function(html) {}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment