Skip to content

Instantly share code, notes, and snippets.

@jdaly13
jdaly13 / gist:5581538
Last active January 18, 2018 20:43
update jquery end method to include parameters
// with this you can write .end(3) instead of .end().end().end()
(function(){
// Define overriding method.
jQuery.fn.end = function(no_of_times){
var prevObject = this.prevObject;
if (!(arguments.length) || (typeof no_of_times !== "number")) {
return this.prevObject || this.constructor(null);
} else {
@jdaly13
jdaly13 / gist:5581679
Created May 15, 2013 04:40
how to chain methods using an object literal (always return this)
var myBooze = {
booze:['wine', 'beer', 'liquor'],
chosenBooze:[],
getBooze: function (type) {
var booze_length = this.booze.length
for (i=0; i < booze_length; i++) {
if (type === this.booze[i]) {
this.chosenBooze.push(this.booze[i]);
return this
} else {
@jdaly13
jdaly13 / gist:5587467
Last active December 17, 2015 09:29
.call example - here we demonstrate that functions themselves are objects and can have properties another function will call the properties of another function
//drinking will call chooseWisely and will look for it's liquor property
//within the liquor property is a wine property that will get output
var drinking = function (who) {
var which_spirit = this.liquor.wine;
console.log(which_spirit) // this will output malbec
return true
};
var chooseWisely = function (ohdeliciouswine, brewski, whiskey) {
@jdaly13
jdaly13 / gist:5592521
Last active December 17, 2015 10:09
prototypal inheritance in javascript example - create an initial object in which other objects will inherit from Use the new Object.create for new browsers and polyfill for older ones
//polyfill for Object.create
if (!Object.create) {
Object.create = function (o) {
if (arguments.length > 1) {
throw new Error('Object.create implementation only accepts the first parameter.');
}
function F() {}
F.prototype = o;
return new F();
};
@jdaly13
jdaly13 / gist:5687482
Created May 31, 2013 19:42
callback function example vanilla js
var beverages = ['juice', 'soda', 'beer']
var func = function (thirsty, array, callback) {
array.push(thirsty);
var first = array[array.length - 1]
return callback(first);
}
func('water', beverages, function (param) {
return param
@jdaly13
jdaly13 / gist:5701015
Created June 3, 2013 20:16
Inheritance Pattern Classical
function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}
function Parent () {}
@jdaly13
jdaly13 / marvel
Created September 4, 2013 22:48
related products module
<script>
//this should probably be added to an external file
setTimeout(function () {
(function (w, d) {
var smallerScreen = "(max-width:990px)"; // this is set in movies.scss as part of media query
var removedTextArray = [];
var removedTextArrayone = [];
var relatedProductsContainer = $("#relatedProducts"); //normally set in ini
var $ul = $('div.btn-dropdown ul');
var liFirstChild = $('div.product_content ul.product_description_list li:first-child');
@jdaly13
jdaly13 / replace
Created September 4, 2013 22:54
start of replacement for marvel module
var relatedProductsContainer = $("#relatedProducts");
relatedProductsContainer.find('article').each(function(index, element) {
var containerHeight = []
var $element = $(element);
var liFirstChild = $element.find('div.product_content ul.product_description_list li:first-child');
var liLastChild = $element.find('div.product_content ul.product_description_list li:last-child');
var liOnlyChild = $element.find('div.product_content ul.product_description_list li:only-child');
var liSpanElement = liFirstChild.children('span:first-child');
var lastLiSpanElement = liLastChild.children('span');
var more = $('<a class="more_or_less more" style="cursor:pointer">more</a>'); //more trigger
@jdaly13
jdaly13 / beEzK.markdown
Created April 10, 2014 03:58
A Pen by James Daly.
/* Word Game jQuery plugin
*
* @param {object} options
* @returns {settings object that override default}
* jQuery plugin
* current dependencies are jQuery ... duh
*/
(function ($) {
$.fn.WordGame = function (options) {
var $container = $(this),