Skip to content

Instantly share code, notes, and snippets.

@colintoh
colintoh / gist:7922113
Last active December 31, 2015 02:39
Refactor
//Call bindEvents in the init()
function bindEvents(){
var $container = $('.container');
$container.on('click','#button--solve',buttonSolveCallback);
$container.on('click','.instruction_close',function(){ $("#blackout-4").fadeOut(); });
@colintoh
colintoh / gist:9523135
Created March 13, 2014 07:03
Iteration
{{#each obj in controller}}
<div class="{{obj.color}}">Some content</div>
{{/each}}
/*
* Predefined Settings
*/
var model = {
daysObj: {
0: [0,1,2,3,4,5,6],
1: [1,2,3,4,5,6,0],
2: [2,3,4,5,6,0,1],
3: [3,4,5,6,0,1,2],
@colintoh
colintoh / for_loop.js
Last active August 29, 2015 14:07
For loop
var arr = "overzealous optimization programming".split("");
for(var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
@colintoh
colintoh / for_loop_cached.js
Last active August 29, 2015 14:07
For loop with cached length
var arr = "overzealous optimization programming".split("");
for(var i = 0, l = arr.length; i < l; i++) {
console.log(arr[i]);
}
@colintoh
colintoh / while_loop.js
Last active August 29, 2015 14:07
While loop
var arr = "overzealous optimization programming".split(""),
i = 0;
while (i < arr.length) {
console.log(arr[i]);
i++;
};
@colintoh
colintoh / log.js
Last active August 29, 2015 14:07
overwrite console
var _consoleLog = console.log;
console.log = function(){
var name = _consoleLog.apply(console,arguments);
var str = "";
for(var index in arguments){
if(Array.isArray(arguments[index])){
arguments[index] = arguments[index].map(function(item){
if(typeof item == "object"){
@colintoh
colintoh / let.js
Last active August 29, 2015 14:07
Block Scope with `let` keyword
var name = "Colin";
let nameBlock = "John";
if(true){
var name = "Max";
let nameBlock = "Min";
console.log(name); //Max
console.log(nameBlock); //Min
}
@colintoh
colintoh / let_for_loop.js
Created October 9, 2014 11:22
Using `let` in loop
var arr = new Array(3);
for (var i = 0; i<arr.length; i++) {
console.log(i); // 0,1,2,3
}
console.log(i); // 3
for(let j = 0; j < arr.length; j++){}
@colintoh
colintoh / destructuring.js
Last active August 13, 2021 07:12
Array Destructuring and Object Destructuring
//Without Array Destructuring
var nameArr = "Colin Toh".split(" ");
var firstName = nameArr[0],
lastName = nameArr[1];
console.log(firstName); // Colin
// With Array Destructuring
var [firstName,lastName] = nameArr;