Skip to content

Instantly share code, notes, and snippets.

View xincici's full-sized avatar
💭
wasting my life and just can not stop

yelin xincici

💭
wasting my life and just can not stop
View GitHub Profile
@xincici
xincici / iteratify.js
Last active September 25, 2019 06:01
destruct object use ... and get a list of data like { key: key, val: val }
Object.prototype[Symbol.iterator] = function() {
var self = this;
var keys = Object.keys(this);
var l = keys.length;
var i = -1;
return {
next: function() {
i++;
var done = i >= l;
var key = keys[i];
@xincici
xincici / EventEmitter.js
Last active September 25, 2019 06:00
just one simple implementation of NodeJS EventEmitter module
var assert = require('assert');
var util = require('util');
function on(thisArg, eventName, fn, isOnce, isPrepend) {
assert(eventName && typeof fn === 'function', 'Need eventName and callback function');
if (eventName !== 'newListener' || thisArg.listenerCount('newListener')){
thisArg.emit('newListener', eventName, fn);
}
if (thisArg.fns[eventName]) {
if (thisArg.fns[eventName].length < thisArg.getMaxListeners()) {
@xincici
xincici / flatObject.js
Last active July 16, 2018 07:16
flat an object
var person = {
name : {
first : 'ye',
last : 'lin'
},
age : 28,
gender : 'male',
birth : {
year : 1987,
month : 6,
@xincici
xincici / throttle-debounce
Created May 29, 2015 08:25
throttle and debounce functions extracted from underscore.js
(function(){
var _ = {};
_.now = Date.now || function(){
return new Date().getTime();
};
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
@xincici
xincici / format
Created May 27, 2015 06:29
数字加千分位
function format(num){
return (num.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}
@xincici
xincici / widthAndHeight
Last active September 17, 2015 03:00
关于网页宽高
网页可见区域宽:window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
网页可见区域高:window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
文档高度:document.body.scrollHeight
文档被滚动高度:document.body.scrollTop
@xincici
xincici / add.js
Last active September 25, 2019 06:06
add function
function add() {
let sum = 0;
Array.from(arguments).forEach(v => sum += v);
function f() {
Array.from(arguments).forEach(v => sum += v);
return f;
}
f.toString = f.valueOf = function() {
return +sum;
}
@xincici
xincici / loadScriptString
Created March 3, 2015 07:30
run script string
//运行插入页面的javascript代码
function loadScriptString(s){
var a = document.createElement('script');
a.type = 'text/javascript';
var t = document.createTextNode(s);
try{
a.appendChild(t);
}catch(e){
a.text = s;
}
@xincici
xincici / lunarLeapMonthOfYear
Created February 26, 2015 09:26
js获取输入年的闰月
function lunarLeapMonthOfYear(year){
var table=[
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,
0x055d2,0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,
0x095b0,0x14977,0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,
0x09570,0x052f2,0x04970,0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,
0x186e3,0x092e0,0x1c8d7,0x0c950,0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,
0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,0x06ca0,0x0b550,0x15355,0x04da0,
0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0,0x0aea6,0x0ab50,0x04b60,
0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,0x096d0,0x04dd5,
@xincici
xincici / getXHR
Created February 26, 2015 09:24
get XMLHttpRequst Object
function getXHR(){
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {