Thanks to "Learning JavaScript Design Pattern" @Addy Osmani
用一句講設計模式
“Pattern 提供“某些特定”情況的可重複利用解法
- 可重複使用
- 可以被傳達 (換句話, 節省developer溝通的時間)
- DRY(Don't Repeat Yourself)
/* | |
依序讀入data, 並以linked list 串接, 印出來 | |
*/ | |
function Node(data) { | |
this.data = data; | |
this.next = null; | |
} |
function TrieNode() { | |
this.key = null; | |
this.children = {}; | |
this.isNode = false; | |
} | |
function insert(word, r) { | |
var start = r; | |
for(var index = 0, le = word.length; index<le; index++) { | |
if (!start.children.hasOwnProperty(word[index])) { | |
var t = new TrieNode(); |
// babel-core 6.x | |
import 'babel-polyfill'; | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import createHistory from 'history/createBrowserHistory'; | |
import pathToRegexp from 'path-to-regexp'; | |
/* | |
your route setting here | |
*/ |
(function() { | |
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || | |
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; | |
window.requestAnimationFrame = requestAnimationFrame; | |
})(); | |
var start = null; | |
var d = document.getElementById("SomeElementYouWantToAnimate"); | |
d.style.backgroundColor = 'red'; |
var fs = require('fs'); | |
var path = require('path'); | |
var targetPath = 'D:\\p'; // 檔案路徑 | |
(function crawl() { | |
var results = []; | |
fs.readdir(targetPath, function (err, list) { | |
if (err) { | |
throw err; | |
} | |
console.log("\n[" + list + "]"); |
function a() { | |
alert('a'); | |
} | |
a(); |
function sequentialCallback (...fns) { | |
return (...args) => { | |
const done = args.pop() | |
const next = (error, ...args) => { | |
if (error) return done(error) | |
if (fns.length) { | |
const fn = fns.shift() | |
return fn(...args, next) | |
} | |
return done(null, ...args) |
Thanks to "Learning JavaScript Design Pattern" @Addy Osmani
用一句講設計模式
“Pattern 提供“某些特定”情況的可重複利用解法
/* | |
問題: 一個陣列裡面的物件要怎麼過濾出相同的key value? | |
例如: var array = [{a:1, b:1}, {a:1, b:2}, {a:2, b:1}, {a:1, b:1}]; | |
(同時考慮 a, b的值) | |
*/ | |
// by @Hao Yu Liao | |
public class MultiKeyDictionary<K1, K2, V> | |
{ | |
private Dictionary<K1, Dictionary<K2, V>> baseDictionary = new Dictionary<K1, Dictionary<K2, V>>(); | |
public IEnumerable<V> Values { get { return baseDictionary.Values.SelectMany(e => e.Values); } } |
/* | |
This code is from | |
https://remysharp.com/2010/07/21/throttling-function-calls/#comment-497362 | |
*/ | |
function throttle(fn, threshhold, scope) { | |
threshhold || (threshhold = 250); | |
var last, | |
deferTimer; | |
return function () { | |
var context = scope || this; |