Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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: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 {