Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
@Williammer
Williammer / javaScript.passValueTricks.js
Last active August 29, 2015 14:02
javaScript.passValueTricks.js - Different condition when passing primitive value and object(and other reference types) to function.
function myfunction(b, context) {
// x is equal to 4
context.b = 5;
//alert(this);
return context;
//alert("x1: "+x);
// x is now equal to 5
}
var x = {
@Williammer
Williammer / javaScript.ReferenceTypesExamples.js
Last active August 29, 2015 14:02
javaScript.ReferenceTypesExamples.js - The most obvious features of the three reference types.
//Reference Types Examples
var ref_fn = function () {
return true;
}
, ref_obj = {
name: 'obj'
}
, ref_arr = ['arr', 2, {
"p": 3
@Williammer
Williammer / jsPatterns.revelation.js
Created June 4, 2014 10:34
jsPatterns.revelation.js
(function () {
var astr = "[object Array]",
toString = Object.prototype.toString;
function isArray(a) {
return toString.call(a) === astr;
}
function indexOf(haystack, needle) {
var i = 0,
@Williammer
Williammer / jsPatterns.memorizeFn.js
Last active August 29, 2015 14:02
jsPatterns.memorizeFn.js
function mem(f) {
var cache = {};
return function(){
var key = arguments.length + Array.prototype.join.call(arguments, ',');
console.log(arguments);//seems like the arguments refers to those in f. No! it refers to those in return function()
if(key in cache) return cache[key];
else return cache[key] = f.apply(this, arguments);
};
}
@Williammer
Williammer / javaScript.argumentsDeepCalc.js
Created June 4, 2014 12:40
javaScript.argumentsDeepCalc.js
function flexisum(a) {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
var el = arguments[i],
num;
if (el == null) {
continue;
} else {
if (isArray(el)) {
num = flexisum.apply(this, el); //recursion -- deep calculate
@Williammer
Williammer / jsPatterns.sandbox.js
Created June 6, 2014 01:45
jsPatterns.sandbox.js
function Sandbox() {
// turning arguments into an array
var args = Array.prototype.slice.call(arguments),
// the last argument is the callback
callback = args.pop(),
// modules can be passed as an array or as individual parameters
modules = (args[0] && typeof args[0] === "string") ? args : args[0],
i;
// make sure the function is called // as a constructor
if (!(this instanceof Sandbox)) {
@Williammer
Williammer / jsPatterns.publicStatic.js
Last active August 29, 2015 14:02
jsPatterns.publicStatic.js
// constructor
var Gadget = function (price) {
this.price = price;
};
// a static method
Gadget.isShiny = function () {
// this always works

var msg = "you bet";
if (this instanceof Gadget) {
@Williammer
Williammer / jsPatterns.privateStatic.js
Created June 6, 2014 01:51
jsPatterns.privateStatic.js
// constructor
var Gadget = (function () {
// static variable/property
var counter = 0,
NewGadget;
// this will become the
// new constructor implementation
NewGadget = function () {
counter += 1;
};
@Williammer
Williammer / jsPatterns.constantFactory.js
Last active August 29, 2015 14:02
jsPatterns.constantFactory.js
var constant = (function () {
var constants = {},
ownProp = Object.prototype.hasOwnProperty,
allowed = {
string: 1,
number: 1,
boolean: 1
},
prefix = (Math.random() + "_").slice(2);
@Williammer
Williammer / jsPatterns.chainedMethod.js
Created June 7, 2014 02:56
jsPatterns.chainedMethod.js - add new methods for an class-like object.
if (typeof Function.prototype.method !== "function") {
Function.prototype.method = function (name, implementation) {
this.prototype[name] = implementation;
return this; // enable chaining function.
};
}
var Person = function (name) {
this.name = name;
}.