Skip to content

Instantly share code, notes, and snippets.

View Williammer's full-sized avatar

William He Williammer

View GitHub Profile
@nolanlawson
nolanlawson / protips.js
Last active August 22, 2024 14:19
Promise protips - stuff I wish I had known when I started with Promises
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");
@Williammer
Williammer / jsDesignPattern.singleton.js
Last active August 29, 2015 14:18
implement a singleton klass with redefine constructor pattern and IIFE.
// Constructor way
function SingletonKlass() {
var instance;
SingletonKlass = function SingletonKlass() {
return instance;
}
SingletonKlass.prototype = this;
instance = new SingletonKlass();
@Williammer
Williammer / jsTimer.centralTimerControl.html
Created June 18, 2014 07:21
jsTimer.centralTimerControl.html - implemented a timer that execute certain action and manage threads.
<body> < div id = "box" style = "position:relative;display:inline;" > Bird! </div>
</body>
<script>
var timers = {
timerID: 0,
timers: [],
add: function (fn) {
this.timers.push(fn);
},
start: function () {
@Williammer
Williammer / jsPatterns.borrowAndBindProp.js
Last active August 29, 2015 14:02
jsPatterns.borrowAndBindProp.js - use apply/call and prototype to implement the function to bind and borrow property from other object.
/* # bind() function - 3 impls that works
* @ sample:
* var newFunc = obj.someFunc.bind(myobj, 1, 2, 3);
*/
//pre ECMA5 bind function:
// 《js Pattern》 version
if (typeof Function.prototype.bind === "undefined") {
Function.prototype.bind = function (thisArg) {
@Williammer
Williammer / jsPatterns.inherit.js
Last active August 29, 2015 14:02
jsPatterns.inherit.js -- including the use of prototype, call/apply, constructor to implement the best solution.
function inherit(Child, Parent) {
Child.prototype = new Parent(); //this is the prototype inhert magic!
}
var holyInherit = (function () {
var F = function () {};
return function (C, P) {
F.prototype = P.prototype;
C.prototype = new F();
C.uper = P.prototype;
@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 / 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
<iframe width="100%" height="300" src="http://jsfiddle.net/Williammer/Yp9gf/1/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<script>
/** javaScript **/
var findNodes = function (callback, callback_obj) {
var i = 10,
nodes = [],
found;
// check if callback is callable
@Williammer
Williammer / javascript.groupTest.html
Last active August 29, 2015 14:01
javascript.groupTest.html - DIY assert() method for testing.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta property="wb:webmaster" content="edcf77ed05a8765f" />
<meta name="viewport" content="width=device-width">
<style>
#result li.passed { color: blue; }
#result li.failed { color: red; }