Skip to content

Instantly share code, notes, and snippets.

View mkuklis's full-sized avatar
🏃‍♂️

Michał Kuklis mkuklis

🏃‍♂️
View GitHub Profile
@mkuklis
mkuklis / gist:1027738
Created June 15, 2011 18:25
JavaScript borrow method pattern
var foo1 = {
name: "hello from foo1",
print: function () {
return this.name;
}
};
var foo2 = {
name: "hello from foo2"
};
@mkuklis
mkuklis / gist:1024385
Created June 14, 2011 05:37
Douglas Crockford's memoizer
var memoizer = function (memo, formula) {
var recur = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n); memo[n] = result;
}
return result;
};
return recur;
};
@mkuklis
mkuklis / gist:1024381
Created June 14, 2011 05:35
Douglas Crockford's functional constructor
var constructor = function (spec, my) {
var that, other private instance variables;
my = my || {};
Add shared variables and functions to my
that = a new object;
Add privileged methods to that
return that;
@mkuklis
mkuklis / gist:1024340
Created June 14, 2011 04:42
Douglas Crockford's method helper
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
return this;
}
};
@mkuklis
mkuklis / gist:1022325
Created June 13, 2011 04:46
falsy values in JavaScript
6 falsy values in JS:
- false
- null
- undefined
- '' // empty string
- 0 // number 0
- NaN
all other values are truthy (including "false")
@mkuklis
mkuklis / gist:1021799
Created June 12, 2011 17:38
exception from jruby when running warbler
# jruby 1.6.2 (ruby-1.9.2-p136) (2011-05-23 e2ea975) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [darwin-x86_64-java]
Michal:rice-on-rails mkuklis$ bundle exec warble config --trace
warble aborted!
stack level too deep
org/jruby/RubyArray.java:1602:in `each'
/Users/mkuklis/.rvm/gems/jruby-1.6.2@rice-on-rails/gems/railties-3.0.8/lib/rails/plugin.rb:51:in `load_deprecated_tasks'
/Users/mkuklis/.rvm/gems/jruby-1.6.2@rice-on-rails/gems/railties-3.0.8/lib/rails/plugin.rb:44:in `load_tasks'
/Users/mkuklis/.rvm/gems/jruby-1.6.2@rice-on-rails/gems/railties-3.0.8/lib/rails/application.rb:140:in `load_tasks'
org/jruby/RubyArray.java:1602:in `each'
@mkuklis
mkuklis / gist:1012380
Created June 7, 2011 14:43
eq to any in JavaScript
function eqToAny (el /*, args */) {
var args = [].slice.call(arguments, 1);
if (args.length == 0) return false;
for (var i = 0, l = args.length; i < l; i++) {
if (el === args[i]) {
return true;
}
}
return false;
}
@mkuklis
mkuklis / gist:1011501
Created June 7, 2011 01:25
JavaScript Hoisting
//1. variable declaration:
// declaration
function foo() {
return true;
var x = 1;
}
// interpretation
funciton foo() {
@mkuklis
mkuklis / gist:1011477
Created June 7, 2011 01:13
JavaScript debounce
function debounce(fn, wait) {
var timeout = null;
return function () {
clearTimeout(timeout);
var args = arguments;
var ctx = this;
timeout = setTimeout(function () {
fn.apply(ctx, args);
}, wait);
}
@mkuklis
mkuklis / gist:1002898
Created June 1, 2011 18:06
TINYeditor
var editor = new TINY.editor.edit('editor', {
id: "editor",
width: 400,
height:200,
cssclass:'te',
controlclass:'tecontrol',
rowclass:'teheader',
dividerclass:'tedivider',
controls:['bold','italic','link','unlink','image','orderedlist','unorderedlist','undo','redo'],
footer:false,