Skip to content

Instantly share code, notes, and snippets.

View PatrickJS's full-sized avatar

PatrickJS PatrickJS

View GitHub Profile
@PatrickJS
PatrickJS / Closures101.js
Created July 21, 2013 20:13
Closures: data hiding with closures, something that encloses it's lexical scope and holds on to variables as a way of passing behavior around. It's behavior that has hidden storage
// Closures
function getCtr() {
var i = 0;
return function() {
console.log(++i);
};
}
var ctr = getCtr();
ctr(); //=> 1
@PatrickJS
PatrickJS / cachingAjaxRequest.js
Last active December 20, 2015 04:08
Caching Ajax Request
;(function() {
var api = {},
$response = $('#response');
$('#ajaxForm').on('submit', function(e) {
e.preventDefault();
var search = $('#title').val();
$response.empty().addClass('loading');
if (!api[search]) {
api[search] = $.ajax({
@PatrickJS
PatrickJS / Closure101.js
Created July 24, 2013 14:54
store your data in closure scope in order to optimise your functions
/*
// Slow way without Closure
var ditgitName = function(n) {
var names = ['zero', 'one', 'two',
'three', 'four', 'five',
'six', 'seven', 'eight', 'nine'];
return names[n]
}
*/
@PatrickJS
PatrickJS / LinkedList.js
Created July 24, 2013 15:06
Create a linked list that stores values without data structures Node in your linked list should be capable of storing any value (Number, String, Object, Function, etc), but you may not store a node's value as a property of any Object, Array, or even Function. The interface is completely up to you, however, the data structure you build must behav…
var Node = function (value, next){
var obj = {
get: function(){
return value;
}
};
if (next) {
next.next = obj;
}
@PatrickJS
PatrickJS / Y_Combinator.js
Last active December 20, 2015 04:29
Y Combinator in Javascript
var y = function(le) {
return function(f) {
return f(f);
}(function(f) {
return le(
function(x) {
return (f(f))(x);
}
);
});
@PatrickJS
PatrickJS / iFrameCrossOrigin.js
Created July 25, 2013 02:04
old Cross Origin with iFrame and jQuery
$(function($) {
if ($.xdomPost) { return; }
var ifc = +new Date(),
css = { position: 'absolute', top: '-1000px', left: '-1000px'},
ieop = $.browser.msie || $.browser.opera && window.opera.version() < 9;
$.xdomPost = function(url, data, options) {
var opts = $.extend({appendTo: 'body'}, options);
var inputs = '',
@PatrickJS
PatrickJS / cachingFibonacci.js
Last active December 20, 2015 18:48
Another method is to memoize the recursive function by extending it with a cache defined inside a closure. This combines the simplicity of the recursive method with the advantages of only calculating each value once.
var fibClosure = (function() {
var cache = [0,1,1];
return function(n) {
if (cache[n] === undefined) {
cache[n] = fibClosure(n - 1) + fibClosure(n - 2);
}
return cache[n];
};
}());
@PatrickJS
PatrickJS / JSFUN.md
Last active December 21, 2015 08:09 — forked from azat-co/JSFUN.md

JS FUNdamentals

If it's not fun, it's not JavaScript.

Expressiveness

Programming languages like BASIC, Python, C has boring machine-like nature which requires developers to write extra code that's not directly related to the solution itself. Think about line numbers in BASIC or interfaces, classes and patterns in Java.

On the other hand JavaScript inherits the best traits of pure mathematics, LISP, C# which lead to a great deal of expressiveness (and fun!).

@PatrickJS
PatrickJS / safeApply.js
Created August 25, 2013 16:58
If you find yourself triggering the '$apply already in progress' error while developing with Angular.JS (for me I find I hit most often when integrating third party plugins that trigger a lot of DOM events), you can use a 'safeApply' method that checks the current phase before executing your function. I usually just monkey patch this into the $s…
.factory('safeApply', ['$rootScope', function($rootScope) {
return function($scope, fn) {
var phase = $scope.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if (fn) {
$scope.$eval(fn);
}
} else {
if (fn) {
@PatrickJS
PatrickJS / closure.coffee
Created August 28, 2013 02:27
Angular components wrapped in IIFE
do(module = angular.module('myApp')) ->
module.service 'myService', ($scope) ->
`
(function (module) {
return module.service('myService', function ($scope) {
// do something
});
}(angular.module('myApp')));