SOURCE

// 1. Object.create
function myCreate(obj){
    function F(){}
    F.__proto__=obj
    return new F()
}

// 2. instanceOf
function myInstanceOf(obj,fn){
    let proto=Object.getPrototypeOf(obj)
    let prototype=fn.prototype
    while(true){
        if(!proto) return false
        if(proto===prototype) return true
        proto=Object.getPrototypeOf(proto)
    }
}

// 3. 手写new操作符
function myNew(){
    let fn=Array.prototype.shift.apply(arguments)
    let obj=new Object()
    obj.__proto__=fn.prototype
    let result=fn.apply(obj,arguments)
    return result&&(typeof result==='object'||typeof result==='function')?result:obj
}

// 4. 手写防抖函数
function myDebounce(fn,wait){
    let timer=null
    return function(){
        if (timer)
            clearTimeout(timer)
        timer=setTimeout(()=>{
            fn.apply(this,arguments)
        },delay)
    }
}

// 5. 手写节流函数
function throttle(fn,wait){
    let previous=0
    return function(){
        let now=+new Date()
        if(now-previous>=wait){
            fn.apply(this,arguments)
            previous=now
        }
    }
}

// 6. 判断类型函数
function getType(o){
    //null和Object类型单独处理,其余用typeof均可判断
    if(o===null){
        return o+''
    }
    if(typeof o==='object'){
        return Object.prototype.toString.call(o).slice(8,-1).toLowerCase()
    }
    return typeof o 
}

// 11. 手写call函数
function myCall(context){
    if(typeof this !=='function') 
        console.log('Type Error!')
    let args=[...arguments].slice(1)
    context=context||window
    context.fn=this
    let result=context.fn(...args)
    delete context.fn
    return result
}

// 12. 手写apply函数
function myApply(context){
    if(typeof this !=='function') 
        console.log('Type Error!')
    let resulit=null
    context=context||window
    context.fn=this
    if(arguments[1])
        result=context.fn(...arguments[1])
    else
        result=context.fn()
    delete context.fn
    return result
}


// 13. 手写bind函数
Function.prototype.myBind = function (context) {
    if (typeof this !== 'function') throw new TypeError('Error')

    const fn = this
    const args = [...arguments].slice(1)

    return new function F() {
        return fn.apply(this instanceof F ? this : context, args.concat(...arguments))
    }
}


// 14. 实现AJAX请求
function myAjax(url) {
    let xhr = new XMLHttpRequest()
    xhr.open('GET', url, true) //是否异步
    xhr.onreadystatechange = function () {
        if (this.readyState !== 4) return
        if (this.status === 200) {
            return this.response
        } else {
            console.error(this.statusText)
        }
    }
    xhr.responseType = 'json'
    xhr.setRequestHeader("Accept", "application/json")
    xhr.send()
}


// 15.使用Promise封装Ajax
function getJson(url) {
    let promise = new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest()
        xhr.open('GET', url, true) //是否异步
        xhr.onreadystatechange = function () {
            if (this.readyState !== 4) return
            if (this.status === 200) {
                resolve(this.response)
            } else {
                reject(new Error(this.statusText))
            }
        }
        xhr.responseType = 'json'
        xhr.setRequestHeader("Accept", "application/json")
        xhr.send()
    })
}


// 16. 手写浅拷贝
function shallowCopy(obj) {
    if (!obj || typeof obj !== 'object') return

    let newObj = Array.isArray(obj) ? [] : {}
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            newObj[key] = obj[key]
        }
    }
    return newObj
}


// 17. 手写深拷贝
function deepCopy(obj) {
    if (!obj || typeof obj !== 'object') return

    let newObj = Array.isArray(obj) ? [] : {}
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            newObj[key] = typeof obj[key] === 'object' ? deepCopy(obj[key]) : obj[key]
        }
    }
    return newObj
}

// 18. 数组乱序
 var arr = [1,2,[3,[4,5]],6,7,8,9,10];
// for (var i = 0; i < arr.length; i++) {
//   const randomIndex = Math.round(Math.random() * (arr.length - 1 - i)) + i;
//   [arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]];
// }
// console.log(arr)

// 19. 数组扁平化
function flatten(arr){
    let result=[]
    for(let i=0;i<arr.length;i++){
        if(Array.isArray(arr[i])){
            result=result.concat(flatten(arr[i]))
        }else{
            result.push(arr[i])
        }
    }
    return result
}

// 20. 数组去重
function removeRepeat(arr){
    return Array.from(new Set(arr))
}

// 21. 字符串repeat若干次
function repeat(str,n){
    return (new Array(n+1)).join(str)
}

// 22. 类数组转化成数组
// (1)
Array.from(arrayLike)
// (2)
Array.prototype.slice.call(arrayLike)
// (3)
Array.prototype.splice.call(arrayLike,0)
// (4)
Array.prototype.concat.apply([],arrayLike)

// 23. json对象转换成对象数
function jsonToTree(data){
    if(typeof data !=='Array')
        return
    let map={}
    let result=[]
    data.forEach((item)=>{
        map[item.id]=item
    })
    data.forEach((item)=>{
        let parent=map[item.pid]
        if(parent){
            (parent.children||(parent.children=[])).push(item)
        }else{
            result.push(item)
        }
    })
    return result
}

console 命令行工具 X clear

                    
>
console