我要我能看懂以上一本書,也不至於如今這個樣子,埃,不說了,說多都是淚。。。。算法
Object.prototype.myCall = function (context, ...params) { if (typeof this !== 'function') { throw TypeError('holp caller is functiom') } let fn = Symbol('fn') let obj = context || window obj[fn] = this let res = obj[fn](...params) delete obj[fn] return res }
Object.prototype.myApply = function (context, params = []) { if (typeof this !== 'function') { throw TypeError('holp caller is functiom') } let fn = Symbol('fn') let obj = context || window obj[fn] = this let res = obj[fn](...params) delete obj[fn] return res }
Object.prototype.myBind = function (context, ...arg) { if (typeof this !== 'function') { throw TypeError('holp caller is functiom') } let obj = context || window return (...arg2) => { this.call(obj, ...arg, ...arg2) } }
function instance(L, R) { let l = L.__proto__ let r = R.prototype while (true) { if (l === null) return false if (l === r) return true l = l.__proto__ } }
function create(obj) { function F() { } F.prototype = obj return new F() }
function myPromise(constructor) { let that = this this.status = 'pendding' this.succVal = '' this.errVal = '' function resovle(val) { if (that.status === 'pendding') { that.succVal = val that.status = 'resolved' } } function reject(val) { if (that.status === 'pendding') { that.errVal = val that.status = 'rejected' } } try { constructor(resovle, reject) } catch (error) { reject(error) } } myPromise.prototype.then = function (succFn, errFn) { let that = this switch (that.status) { case 'resolved': succFn(that.succVal) break case 'rejected': errFn(that.errVal) break } }
function myNew(context, ...params) { let obj = Object.create(context.prototype) let res = context.apply(obj, params) return typeof res === 'object' ? res : obj }
function deepClone(p, c) { for (let prop in p) { if (typeof p[prop] === 'object') { c[prop] = p[prop] instanceof Array ? [] : {} deepClone(p[prop], v[prop]) } else { c[prop] = p[prop] } } }
function Animal(name) { this.name = name } Animal.prototype.sayName = function () { console.log(this.name) } function Dog(name, age) { Animal.call(this, name) this.age = age } Dog.prototype = Object.create(Animal.prototype) Dog.prototype.constructor = "Dog" Dog.prototype.sayAge = function () { console.log(this.age) }
function binarySearch(arr, start, end, value) { if (start > end) { return -1 } let middle = parseInt((start + end) / 2) if (value < arr[middle]) { end = middle + 1 return binarySearch(arr, start, end, value) } else if (value > arr[middle]) { start = middle - 1 return binarySearch(arr, start, end, value) } else if (value === arr[middle]) { return middle } }
function quickSort(arr) { if (arr.length <= 1) { return arr; } let middle = Math.floor(arr / 2) let value = arr.splice(middle, 1)[0] let left = [], right = [] for (let i = 0; i < arr.length; i++) { if (arr[i] > value) { right.push(arr[i]) } else if (arr[i] < value) { left.push(arr[i]) } } return quickSort(left).concat([value], quickSort(right)) }
function bubbleSort(arr) { let len = arr.length let temp for (let i = 0; i < len; i++) { for (let j = i + 1; j < len; j++) { if (arr[i] < arr[j]) { temp = arr[i] arr[i] = arr[j] arr[j] = temp } } } return arr }
function selectSort(arr) { let len = arr.length let minIndex, temp for (let i = 0; i < len; i++) { minIndex = i for (let j = i + 1; j < len; j++) { if (arr[minIndex] > arr[j]) { minIndex = j } } temp = arr[minIndex] arr[minIndex] = arr[i] arr[i] = temp } return arr }
let xhr = new XMLHttpRequest() xhr.open(Get,'xxx.js',false) xhr.onreadystatechange = function () { if (xhr.stutas === 200 && xhr.readyState === 4) { console.log(xhr.responseText) } }
function observer() { let msg = {} return { register: function (type, fn) { if (!msg[type]) { msg[type] = [fn] } else { msg[type].push(fn) } }, dispatch: function (type, params) { if (!msg[type]) return false let arg = { type, params } let len = msg[type].length - 1 for (let i = 0; i <= len; i++) { msg[type][i].call(this, arr) } }, remove: function (type, fn) { if (Array.isArray(msg[type])) { let len = msg[type].length - 1 for (let i = length; i >= 0; i--) { msg[type] && msg[type][i] === fn && msg[type].splice(i, 1) } } } } }
function fibo(n) { if (n === 1 || n === 2) { return 1 } return fibo(n - 2) + fibo(n - 1) }
function questStep(arr) { let key = {} let value = [] arr.forEach(element => { if (!(element in key)) { key[element] = true value.push(element) } }) return value }
let antiShake = function () { let timer = null return function () { timer && clearTimeout(timer) timer = setTimeout(() => { console.log('防抖成功') timer = null }, 2000) } }
function throttle() { let timer = null return () => { if (!timer) { timer = setTimeout(() => { console.log('節流成功') timer = null }, 2000) } } }
function bothwayBind(fromKey, container, obj, key) { Object.defineProperty(obj, key, { set(val) { fromKey.value = val container.innerHTML = val } }) fromKey.onkeyup = function (e) { obj[key] = e.target.value } }
若是對你有幫助,我只須要你幫我作一件事,留在你的👍,讓更多人可以看到,謝謝🙏。app