否有中间的调度中心。
class EventBus { constructor () { this.events = Object.create(null) } on (event, fn) { this.events.event = this.events.event || [] this.events.event.push(fn) } off (event, fn) { const index = (this.events.event || []).indexOf(fn) if (index < -1) { return } else { this.events.event.splice(index, 1) } } fire (event) { this.events.event.forEach(fn => fn()) } } var bus = new EventBus() bus.on(‘onclick‘, function () { console.log(‘click1 fire‘) }) bus.on(‘onclick‘, fn=function () { console.log(‘click2 fire‘) }) bus.fire(‘onclick‘)
class Observer { constructor (fn) { this.update = fn } } class Subject { constructor () { this.observers = [] } addObserver (observer) { this.observers.push(observer) } removeObserver (observer) { const delIndex = this.observers.indexOf(observer) this.observers.splice(delIndex, 1) } notify () { this.observers.forEach(observer => { observer.update() }) } } var subject = new Subject() var ob1 = new Observer(function () { console.log(‘ob1 callback run‘) }) subject.addObserver(ob1) var ob2 = new Observer(function () { console.log(‘ob2 callback run‘) }) subject.addObserver(ob2) subject.notify()
// 防抖,在一定事件内,多次触发事件,会重新计算时间进行执行 // 防抖 input 校验 模糊搜索 resize function debounce (fn,delay) { // 记录上一次的延时器 var timer = null; return function () { // 清除上一次的延时器 clearTimeout(timer); // 重新设置新的延时器 timer = setTimeout(function () { fn.apply(this); },delay) } }
function throttle (fn, delay) { var lastTime = 0; return function () { var nowTime = Date.now(); if (nowTime - lastTime > delay) { fn.call(this); lastTime = nowTime; } } }
一:JSON.parse(JSON.toString(obj)); // 无法拷贝内置方法 二 function deepCopy (obj) { let result = obj; if (typeof(obj) === ‘string‘ && obj !== null) { result = Object.prototype.toString.call(obj) === ‘[object Array]‘ ? [] : {}; for (let prop in obj) { result[prop] = deepCopy(obj[prop]); } } return result; } var obj = { name: ‘xiao‘, age: 24, run :{ function () { console.log(1) } } };
function deepCopy (obj) { // 排除null if (obj === null) { return null; } // 判断是数组还是对象 var result = Array.isArray(obj) ? [] : {}; // 循环 for (let key in obj) { // 判断属性是否存在 if(obj.hasOwnProperty(key)) { if (typeof obj[key] === ‘object‘) { // 如果还是对象,再次调用此方法 result[key] = deepCopy(obj[key]); } else { result[key] = obj[key] } } } }
var arr =[1,2,3,4,5,2,3]; var arr1 = [... new Set(arr)]
var arr = [1,2,3,4,2,1]; var arr1 = arr.reduce(function(ar, item){ if (!ar.includes(item)) { ar.push(item); } return ar; },[]);
function reverse(str) { return str .split(‘‘) .reverse() .join(‘‘) } console.log(reverse(str));
var ary=[1,4,7,8,12,34,67,88,99,100] function getIndex(arr,num) { var len = arr.length; var st = 0; var end = len-1; while(str<end){ var mid = Math.floor((st+end)/2); if (num = arr[mid]) { return mid; } else if (num > arr[mid]) { st = mid +1; } else { end = mid-1; } } }
// 冒泡排序 //从小到大 function BubbleSort(arr){ var i,j,temp; for(i=0;i<arr.length-1;i++){ for(j=i+1;j<arr.length;j++){ if(arr[i]>arr[j]){ temp=arr[j]; arr[j]=arr[i]; arr[i]=temp; } } } return arr; } var arr=[10,7,9,11,22,33,4,2,0,1000]; BubbleSort(arr); console.log(arr); //[0, 2, 4, 7, 9, 10, 11, 22, 33, 1000]
function a() { this.name = ‘张三‘; } a.prototype.getName = function() { return this.name; } var instance = new a(); console.log(instance.getName());//‘张三‘
原文:https://www.cnblogs.com/jcxfighting/p/11668343.html