可能這裏有你想要的源碼實現

WechatIMG1.jpeg
我要我能看懂以上一本書,也不至於如今這個樣子,埃,不說了,說多都是淚。。。。算法

1.call

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

}

2.apply

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

}

3.bind

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)

    }

}

4.instanceOf

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__

    }

}

5.Objeact.create

function create(obj) {

    function F() { }

    F.prototype = obj

    return new F()

}

6.promiss

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

    }

}

7.new

function myNew(context, ...params) {

   let obj = Object.create(context.prototype)

   let res = context.apply(obj, params)

   return typeof res === 'object' ? res : obj

}

8.深拷貝

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]

        }

    }

}

9.es5繼承

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)

}

10.二分查找

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

    }

}

11.快排

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))

}

12.冒泡排序

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

}

13.選擇排序

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

}

14.原生Ajax

let xhr = new XMLHttpRequest()

xhr.open(Get,'xxx.js',false)

xhr.onreadystatechange = function () {

   if (xhr.stutas === 200 && xhr.readyState === 4) {

       console.log(xhr.responseText)

   }

}

15.訂閱發佈模式

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)

                }

            }

        }

    }

}

16.斐波那契算法

function fibo(n) {

    if (n === 1 || n === 2) {

        return 1

    }

    return fibo(n - 2) + fibo(n - 1)

}

17.去重

function questStep(arr) {

    let key = {}

    let value = []

    arr.forEach(element => {

        if (!(element in key)) {

            key[element] = true

            value.push(element)

        }

    })

    return value

}

18.防抖

let antiShake = function () {

    let timer = null

    return function () {

        timer && clearTimeout(timer)

        timer = setTimeout(() => {

            console.log('防抖成功')

            timer = null
            
        }, 2000)


    }
}

19.節流

function throttle() {

    let timer = null

    return () => {

        if (!timer) {

            timer = setTimeout(() => {

                console.log('節流成功')

                timer = null

            }, 2000)

        }

    }
}

20.雙向綁定

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

相關文章
相關標籤/搜索