代码拉取完成,页面将自动刷新
// 防抖
function debounce(fn, wait) {
let timer;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments);
}, wait);
};
}
// 节流
function throttle(fn, wait) {
let prev = 0;
return function() {
let now = Date.now();
if (now - prev > wait) {
prev = now;
fn.apply(this.arguments);
}
};
}
// 深拷贝
function deepClone(obj, map = new WeakMap()) {
if (typeof obj !== "object" || !obj) {
return obj;
}
if (map.get(obj)) {
return map.get(obj);
}
let target = new obj.constructor();
map.set(obj, target);
for (let key in obj) {
if (obj.getOwnPropertyNames(key)) {
target[key] = deepClone(obj[key]);
}
}
return target;
}
// 改变this的指向
Function.prototype.myCall = function(context, ...args) {
let fn = Symbol.for("fn");
context = context || window;
context.fn = this;
let ret = context.fn(...args);
delete context.fn;
return ret;
};
Function.prototype.MyBind = function(context, ...args) {
let that = this;
let fBound = function() {
that.apply(
this instanceof that ? this : context,
args.concat(...arguments)
);
};
fBound.prototype = Object.create(that.prototype);
return fBound;
};
function sleep(wait) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, wait);
});
}
// 最大并发数
class Semaphore {
constructor(available) {
this.available = available;
this.waiters = [];
this._continue = this._continue.bind(this);
}
// 请求
tack(callback) {
if (this.available > 0) {
this.available--;
callback();
} else {
this.waiters.push(callback);
}
}
// 释放
leave() {
this.available++;
if (this.waiters.length > 0) {
process.nextTick(this._continue);
}
}
_continue() {
if (this.available > 0) {
if (this.waiters.length > 0) {
this.available--;
const callback = this.waiters.pop();
callback();
}
}
}
}
// jsonp
function jsonp(url, params, cb) {
return new Promise((resolve, reject) => {
window[cb] = function(data){
resolve(data)
document.body.removeChild(script)
}
params = {...params,cb}
let arrs = []
for(let key in params){
arrs.push(`${key}=${params[key]}`)
}
let script = document.createElement('script')
script.src = `${url}?${arrs.join('&')}`
document.body.appendChild(script)
});
}
// Promise
// instanceof
function instanceOf(left,right){
left = left.__proto__
while(left){
if(left == right.prototype){
return true
}
left = left.__proto__
}
return false
}
// new
function myNew(Con,...args){
let obj = Object.create(Con.prototype)
let ret = Con.call(obj,...args)
return ret instanceof Object ? ret : obj
}
// flat
Array.prototype.flat = function(){
let result = []
let _this = this
function _flatten(arr){
for(let i = 0;i<arr.length;i++){
let item = arr[i]
if(Array.isArray(item)){
_flatten(item)
}else{
result.push(item)
}
}
}
_flatten(_this)
return result
}
// lazyMan
class LazyManClass{
constructor(name){
this.taskList = []
this.name = name
console.log(`Hi I am ${this.name}`)
setTimeout(()=>{
this.next()
},0)
}
eat(name){
let that = this
let fn = (function(n){
return function(){
console.log(`I am eating ${n}`)
that.next()
}
})(name)
this.taskList.push(fn)
return this
}
sleepFirst(time){
let that = this
let fn = (function(t){
return function(){
setTimeout(()=>{
console.log(`等待了${t}秒`)
that.next()
},t*1000)
}
})(time)
this.taskList.unshift(fn)
return this
}
sleep(time){
let that = this
let fn = (function(t){
return function(){
setTimeout(()=>{
console.log(`等待了${t}秒`)
that.next()
},t*1000)
}
})(time)
this.taskList.push(fn)
return this
}
next(){
let fn = this.taskList.shift()
fn && fn()
}
}
// curry
function curry(fn,...args){
return args.length < fn.length ? (...extraArgs) => curry(fn,...args,...extraArgs) : fn(...args)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。