const PENDING = 'pending' const RESOLVED = 'resolved' const REJECTED = 'rejected' function MyPromise(fn){ const that = this that.state = PENDING that.value = null that.resolvedCallbacks = [] that.rejectedCallbacks = [] function resolve(value) { if(that.state === PENDING) { that.state = RESOLVED that.value = value that.resolvedCallbacks.map(cb => cb(that.value)) } } function reject(value) { if(that.state === PENDING){ that.state = REJECTED that.value = value; that.rejectedCallbacks.map(cb => cb(that.value)); } } try { fn(resolve, reject) } catch (e) { reject(e) } } MyPromise.prototype.then = function(onFulfilled, onRejected) { const that = this //对传入的两个参数作判断,若是不是函数将其转为函数 onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v // onFulfilled = v => v onRejected = typeof onRejected === 'function' ? onRejected : r => { throw r } if(that.state === PENDING) { that.resolvedCallbacks.push(onFulfilled) that.rejectedCallbacks.push(onRejected) } else if(that.state === RESOLVED) { onFulfilled(that.value) } else { onRejected(that.value) } } new MyPromise((resolve, reject) => { setTimeout(() => { resolve('成功的回调数据') }, 1000) }).then(value => { console.log('Promise.then: ', value) })
1.末尾加上末尾,若是大于等于10,产生进位。若是没有进位就是0.2.倒数第二位加上倒数第二位,再加上进位,若是大于等于10,产生进位。3.倒数第三位加上倒数第三位,再加上进位,若是大于等于10,产生进位。。。。直到加完数字小的位数。第一循环结束,进行第二循环。第二循环是指,数字较小(或者长度较短)的数字已经加完结束了。剩下的就是第一循环加完后,剩下的进位与剩下的相加。node
//bigNumberA和bigNumberB使用字符串存储,不然会自动转化为科学计数 let bigNumberAdd = (bigNumberA, bigNumberB) => { let A = (bigNumberA + '').split(''); let B = (bigNumberB + '').split(''); let aLen = A.length, bLen = B.length, cLen = Math.max(aLen, bLen) + 1; let result = [], prefix = 0; for (let i = 0; i< cLen -1; i++ ) { let a = aLen - i - 1 >= 0 ? parseInt(A[aLen - i - 1]) : 0, b = bLen - i - 1 >= 0 ? parseInt(B[bLen - i - 1]) : 0; result[i] = (a + b + prefix) % 10; prefix = Math.floor((a + b + prefix) / 10); } return result.reverse().join(''); }; bigNumberAdd('45486646468484544661134868968','544545867466464646');
animation easereact
.triangle{ width:0; height:0; border-left:50px solid transparent; border-right:50px solid transparent; border-buttom:100px solid yellow; }
var reverseList=function(head){ if(head===null||head.next===null){ return head; } var new_head=reverseList(head.next); head.next.next=head; head.next=null; return new_head; };
函数防抖就是在函数须要频繁触发的状况下,只有足够的空闲时间,才执行一次。
典型应用nginx
拖拽web
function debounce(handler, delay){ delay = delay || 300; var timer = null; return function(){ var _self = this, _args = arguments; clearTimeout(timer); timer = setTimeout(function(){ handler.apply(_self, _args); }, delay); } }
一个函数只有在大于执行周期时才执行,周期内调用不执行。好像水滴积攒到必定程度才会触发一次下落同样。
典型应用:正则表达式
页面滚动算法
function throttle(handler, wait){数据库
wait = wait || 300; var lastTime = 0; return function(){ var _self = this, _args = arguments; var nowTime = new Date().getTime(); if((nowTime - lastTime) > wait){ handler.apply(_self, _args); lastTime = nowTime; } } }
9开头数组
[1, [2, 3, [4, 5]]] ------> [1, 2, 3, 4, 5]
递归法:promise
function flatten(arr) { let res = []; arr.map(item => { if(Array.isArray(item)) { res = res.concat(flatten(item)) } else { res.push(item); } }) return res; }
cookie,session,localStorage,sessionStorage缓存
Expires、Cache-Control、(强缓存)
Last-Modified、Etag。(协商缓存)
map、filter、concat、slice
WebSocket发送图片时候,我建议是进行图片压缩,最好把图片压缩在100K之内。
websocket发送大文件的时候,通过个人测试,若是客户端链接的服务器端的量比较大,分段发送,而后保存到数据库。
进程是操做系统资源分配的基本单位,而线程是任务调度和执行的基本单位。个程序至少有一个进程,一个进程至少有一个线程。