Created
October 16, 2018 19:46
-
-
Save cferdinandi/904c089600a969b705fc14286934a0cf to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>ConsolidateJS</title> | |
<style type="text/css"> | |
.bg-purple { | |
background-color: rebeccapurple; | |
} | |
.white { | |
color: white; | |
} | |
</style> | |
</head> | |
<body> | |
<p id="p1" class="turkey">#p1 .turkey</p> | |
<p id="p2" class="turkey">#p2 .turkey</p> | |
<p id="p3" class="turkey">#p3 .turkey</p> | |
<p id="p4" class="tuna">#p4 .tuna</p> | |
<p id="p5" class="tuna">#p5 .tuna</p> | |
<script type="text/javascript"> | |
var f$ = (function () { | |
'use strict'; | |
/** | |
* Create the constructor | |
*/ | |
var Constructor = function (selector) { | |
this.elems = document.querySelectorAll(selector); | |
}; | |
/** | |
* Do ajax stuff | |
* @param {String} url The URL to get | |
*/ | |
Constructor.prototype.ajax = function (url) { | |
// Do some XHR/Fetch thing here | |
console.log(url); | |
}; | |
/** | |
* Run a callback on each item | |
* @param {Function} callback The callback function to run | |
*/ | |
Constructor.prototype.each = function (callback) { | |
if (!callback || typeof callback !== 'function') return; | |
for (var i = 0; i < this.elems.length; i++) { | |
callback(this.elems[i], i, this.elems); | |
} | |
return this; | |
}; | |
/** | |
* Add a class to elements | |
* @param {String} className The class name | |
*/ | |
Constructor.prototype.addClass = function (className) { | |
this.each(function (item) { | |
item.classList.add(className); | |
}); | |
return this; | |
}; | |
/** | |
* Remove a class to elements | |
* @param {String} className The class name | |
*/ | |
Constructor.prototype.removeClass = function (className) { | |
this.each(function (item) { | |
item.classList.remove(className); | |
}); | |
return this; | |
}; | |
var init = function (selector) { | |
return new Constructor(selector); | |
}; | |
// | |
// Return the constructor | |
// | |
return init; | |
})(); | |
var p1 = f$('#p1'); | |
p1.elems[0].style.fontStyle = 'italic'; | |
p1.ajax('https://gomakethings.com'); | |
var tuna = f$('.tuna'); | |
tuna.each(function (sandwich) { | |
sandwich.style.fontWeight = 'bold'; | |
}); | |
f$('.turkey').addClass('bg-purple').addClass('white'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Chris. This is exactly I was looking for to understand.