Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
@Williammer
Williammer / jsPerform.joinJs.js
Created June 9, 2014 08:57
jsPerform.joinJs.js - join all the external javaScript files, and execute them after the core js file is loaded.
var script = document.createElement("script");
script.src = "all_20100426.js";
document.documentElement.firstChild.appendChild(script);
var mod = {
buffer: [];
}
var inline_script = 'some_inline scripts';
//more inline scripts...
@Williammer
Williammer / jsPerform.preload.js
Created June 9, 2014 09:11
jsPerform.preload.js - preload certain element at the right time, such as user login;
var preload;
if ( /*@cc_on!@*/ false) { // IE sniffing with conditional comments
preload = function (file) {
new Image().src = file;
};
} else {
preload = function (file) {
var obj = document.createElement('object'),
body = document.body;
@Williammer
Williammer / javaScript.objCacheFn.js
Created June 10, 2014 03:53
javaScript.objCacheFn.js - use object to cache unique and related functions, with the key-value structure similar to array in PHP.
var store = {
nextId: 1,
cache: {},
add: function (fn) {
if (!fn.id) {
fn.id = this.nextId;
this.nextId += 1;
return !!(this.cache[fn.id] = fn);
}
}
@Williammer
Williammer / javaScript.closureCacheFn.js
Created June 10, 2014 04:32
javaScript.closureCacheFn.js - to cache fns with closure, privacy of cache and nextId improved.
var store = (function () {
var nextId = 1,
cache = {};
return {
addFn: function (fn) {
if (!fn.id) {
fn.id = nextId;
nextId += 1;
return !!(cache[fn.id] = fn);
} else {
@Williammer
Williammer / javaScript.fnObjCacheFn.js
Created June 10, 2014 05:10
javaScript.fnObjCacheFn.js - the function cache itself with its own property.
function isPrime(value) {
if (!isPrime.anwers) isPrime.answers = {};
if (isPrime.answers[value] != null) {
return isPrime.answers[value];
}
var prime = value != 1; // 1 can never be prime
for (var i = 2; i < value; i++) {
if (value % i == 0) {
prime = false;
break;
@Williammer
Williammer / javaScript.closureTimer.js
Created June 13, 2014 08:33
javaScript.closureTimer.js - use closure on the timer function, which repeat itself 100 times(similar to 100 times of different invocation) to modify the private value.
function animateIt(elementId) {
var elem = document.getElementById(elementId);
var tick = 0;
var timer = setInterval(function () {
if (tick < 100) {
elem.style.left = elem.style.top = tick + "px";
tick++;
} else {
clearInterval(timer);
assert(tick == 100,
@Williammer
Williammer / jsPattern.curryExample.js
Created June 13, 2014 08:51
jsPattern.curryExample.js - one simple curry example to partially add parameters, with closure.
function add(x, y) {
if (typeof y === "undefined") { // partial
return function (y) {
return x + y;
};
}
// full application
return x + y;
}
@Williammer
Williammer / jsPattern.currySchonfinkelize.js
Created June 13, 2014 08:57
jsPattern.currySchonfinkelize.js - one general purpose curry function to partially exec arguments to the fn argument.
function schonfinkelize(fn) {
var slice = Array.prototype.slice,
stored_args = slice.call(arguments, 1);
return function () {
var new_args = slice.call(arguments),
args = stored_args.concat(new_args);
return fn.apply(null, args);
};
}
@Williammer
Williammer / jsPattern.prototypeCacheVal.js
Last active August 29, 2015 14:02
jsPattern.prototypeCacheVal.js - cache value with a new Function.prototype method.
Function.prototype.memoized = function (key) {
this._values = this._values || {};
return this._values[key] !== undefined ? this._values[key] : this._values[key] = this.apply(this, arguments);
};
Function.prototype.memoize = function () {
var fn = this;
return function () {
return fn.memoized.apply(fn, arguments);
};
};
@Williammer
Williammer / jsPattern.wrapFnWithNewFn.js
Created June 14, 2014 14:54
jsPattern.wrapFnWithNewFn.js - to replace the function with a new function at certain situation.
function wrap(object, method, wrapper) {
var fn = object[method];
return object[method] = function () {
return wrapper.apply(this, [fn.bind(this)].concat(
Array.prototype.slice.call(arguments)));
};
}
if (Prototype.Browser.Opera) {
wrap(Element.Methods, "readAttribute",