Skip to content

Instantly share code, notes, and snippets.

View CatTail's full-sized avatar
😑

Chiyu Zhong CatTail

😑
View GitHub Profile
@CatTail
CatTail / closure.js
Last active December 17, 2015 01:49
浅谈Javascript闭包
// uuid generator, 通过闭包在当前页面状态产生全局唯一的序列
var uuid = (function(){
var id = 0;
return function () {
return id++;
};
}());
uuid(); // 0
uuid(); // 1
uuid(); // 2 ...
@CatTail
CatTail / curry.js
Created May 9, 2013 14:04
Curry and uncurry
Function.prototype.uncurryThis = function() {
var f = this;
return function() {
var a = arguments, b = [].slice.call(a, 1);
return f.apply(a[0], b);
};
};
Function.prototype.curry = function() {
var fn = this;
@CatTail
CatTail / typal.js
Last active April 3, 2023 20:37
Javascript prototypal/classical/mixins
/*
* Introduces a typal object to make classical/prototypal patterns easier
* Plus some AOP sugar
*
* By Zachary Carter <zach@carter.name>
* MIT Licensed
* */
var typal = (function () {
@CatTail
CatTail / shortlink.sh
Created May 28, 2013 15:50
命令行创建github链接短链
@CatTail
CatTail / cp-progress.sh
Last active December 17, 2015 19:58
cp command with progress bar
cp_p()
{
strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
| awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
@CatTail
CatTail / preserver.js
Created June 13, 2013 16:31
Provide a way to preserve module with different versions.
var module = {};
// global on the server, window in the browser
var root, previous_module;
root = this;
if (root != null) {
previous_module = root.module;
}
@CatTail
CatTail / module.js
Last active January 17, 2016 04:22
Cross plantform module export
(function() {
var root = this;
// AMD / RequireJS
if (typeof define !== 'undefined' && define.amd) {
define('modulename', [], function () {
return global;
});
}
// Node.js
else if (typeof module !== 'undefined' && module.exports) {
@CatTail
CatTail / inception.js
Created July 12, 2013 02:22
Javascript code explain closure by movie inception.
/**
* Platform cattail@qudian.laptop Arch Linux
* Date 2013-07-12 09:43:15
* @author zhongchiyu@gmail.com (cattail)
*
* @fileoverview Javascript code explain closure by movie inception.
* dream - function
* everything in dream including secret - scope
* people inside dream - executable code
*/
@CatTail
CatTail / bind.js
Created July 12, 2013 08:27
Bind function to this OR bind this to function ?!
Function.prototype.curry = function () {
var fn = this;
var args = [].slice.call(arguments, 0);
return function () {
return fn.apply(this, args.concat([].slice.call(arguments, 0)));
};
};
var bind = function (func, thisArg) {
return function () {
@CatTail
CatTail / constructor.js
Created July 16, 2013 15:42
Scope-Safe Constructors
/**
* Code example from <professional javascript for web developers v3 p733>
*/
// problem
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
}