Skip to content

Instantly share code, notes, and snippets.

View zhanhongtao's full-sized avatar
😀
Out sick

redky zhanhongtao

😀
Out sick
View GitHub Profile
@zhanhongtao
zhanhongtao / define_function.js
Last active December 22, 2015 06:49
函数定义和函数调用
// 函数声明
function _name() {
//...
}
// 函数表达式
var _name = function() {
//...
};
@zhanhongtao
zhanhongtao / auto-run.js
Last active December 22, 2015 06:49
匿名函数
// 自执行函数
;(function() {
//...
})();
// dom 绑定.
document.onclick = function () {
// ....
};
@zhanhongtao
zhanhongtao / gist:6445945
Created September 5, 2013 04:01
partial
// partial
;(function() {
var BASE_FONT_SIZE = 100;
var zoom = partial(function( a, b ) {
return a + b;
}, BASE_FONT_SIZE );
var result = zoom( -30 ); // 70
var result2 = zoom( 50 ); // 150
@zhanhongtao
zhanhongtao / type.js
Last active December 22, 2015 08:49
type
function type(s) {
return Object.prototype.toString.call(s).slice(8,-1).toLowerCase();
}
console.assert( type( null ) == 'null', 'null' );
console.assert( type( undefined ) == 'undefined', 'undefined' );
console.assert( type( [] ) == 'array', '[]' );
console.assert( type( {} ) == 'object', 'object' );
console.assert( type( false ) == 'boolean', 'false' );
console.assert( type( 1.2 ) == 'number', '1.2' );
@zhanhongtao
zhanhongtao / call-apply.js
Last active December 22, 2015 08:49
Math.min & Math.max call/apply
var result = 80;
var MAX_VALUE = 50;
var MIN_VALUE = 0;
// result 值不能小于 0
result = Math.max.call( Math, MIN_VALUE, result );
result = Math.min.apply( Math, [ MAX_VALUE, result ] );
console.log( result );
/*
@return {object}
{
on: 开权限
off: 取消权限
done: 有权限时, 执行操作
}
*/
function create( func, check, time ) {
var flag = false;
@zhanhongtao
zhanhongtao / remove-dom-by-class
Created October 8, 2013 06:57
remove element by class
function removeDomByClass( classname ) {
var boxes = document.getElementsByClassName( classname );
for ( var i = 0, l = boxes.length; i < l; i++ ) {
var box = boxes[0];
if ( box && box.parentNode ) {
box.parentNode.removeChild( box );
}
}
}
@zhanhongtao
zhanhongtao / log
Created October 10, 2013 09:07
console.log
var type = function(s) {
return Object.prototype.toString.call(s).slice(8,-1).toLowerCase();
};
var log = function() {
var color = 'background: #000;color: yellow;padding: 0 3px';
var string = '%c';//'%c';
for ( var i = 0, l = arguments.length; i < l; i++ ) {
var t = type(arguments[i]);
switch(t) {
var f;
f = function ( n ) {
if ( n === 0 ) return 0;
else
return n + f(n-1);
};
var a = f(3);
console.log( 'a: ', a );
@zhanhongtao
zhanhongtao / permgen.js
Created October 13, 2013 03:01
全排列算法 - JavaScript 实现
var a = [ 1, 2, 3 ];
function change( a, i, j) {
var t = a[i];
a[i] = a[j];
a[j] = t;
}
function permgen( a, n ) {
n = n == null ? a.length : n;