js的tree化复习

73 阅读3分钟

基本数据类型 number string boolean undefined null object(array function) bigInt symbol

延伸出-number的操作方法 string的操作方法 arrry的操作方法

延伸出-数据类型判断 typeof instansof Object.prototype.toString.call() === '[Object 数据类型]'

再延伸出-复制数据-深克隆浅克隆

其中有递归 判断数据类型 原型链

所用皆为函数-普通函数;箭头函数;匿名函数;自执行函数;闭包;

再有创建函数 new一个东西 这其中包括this指向类 call apply bind

call apply bind区别和用法
    call
    
    // 用法: Function.call(obj, [param1[, param2[, ...[, paramN]]]])
    
    -   调用call的对象,必须是个函数Function

    -   call的第一个参数,是一个对象,Function的调用者,将会指向这个对象。如果不传,则默认为全局对象window

    -   第二个参数开始,可以接收任意个参数,每个参数会映射到相应位置的Function的参数上。

    -   但是如果将所有的参数作为数组传入,他们会作为一个整体映射到Function对应的第一个参数上,之后参数都为空
 
    function func(a,b,c) {} // 箭头函数无法改变 this 的指向 
    func.call(obj, 1,2,3) // func接收到的参数为1,2,3
    func.call(obj, [1,2,3]) // func接收到的参数为[1,2,3], undefined, undefined
    
    apply
       // 用法:Function.apply(obj[, argArray])
       // -   它的调用者必须是函数Function,并且只接受两个参数,第一个参数的规则与call一致
       // -   第二个参数,必须是数组或者类数组,他们会被转换成类数组,传入到Function中,并且会被映射到Function对应的参数上,
   
       func.apply(obj, [1,2,3]) // func接收到的参数是1,2,3 
       func.apply(obj, {0:1, 1:2, 2:3, length: 3}) // func接收到的参数是1,2,3
    
###call和apply的用途

##call的使用场景

-   对象的继承


function superClass() {
    this.a = 1
    this.print = function() {
        console.log(this.a)
    }
}
function subClass() {
    superClass.call(this)
    this.print()
}
subClass() // 1

-   借用方法

    -   类数组,如果想使用Array原型链上的方法
    -   `let demoNodes = Array.prototype.slice.call(document.getElementsByTagName("*"))`

#### apply 的用法

-   Math.max,用来获取数组中最大的一项

    -   `let max = Math.max.apply(null, array)`

-   实现两个数组合并


let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
Array.prototype.push.apply(arr1, arr2)
console.log(arr1) // [1, 2, 3, 4, 5, 6]

bind 

// Function.bind(thisArg, arg1, arg2])
-   bind方法与apply、call比较类似,也能改变函数体内的this指向,不同的是,bind方法的返回值是函数,并且需要稍后调用,才会执行。而apply和call则是立即调用
-   如果bind的第一个参数是null或者undefinedthis就指向全局对象window


function add (a, b) {
    return a + b;
}
function sub (a, b) {
    return a - b;
}
add.bind(sub, 5, 3); // 这时,并不会返回 8
add.bind(sub, 5, 3)(); // 调用后,返回 8

总结:
-   call 和 apply 的主要作用,是改变对象的执行上下文,并且是立即执行的。它们在参数上的写法略有区别。
-   bind 也能改变对象的执行上下文,它与 call 和 apply 不同的是,返回值是一个函数,并且需要稍后再调用一下,才会执行。

再有js内置函数date类 set类 map类

再有辅助函数执行机制的promise try catch async awiat

运行完成后有垃圾回收 标记回收和引用回收

特殊类setTimeout setTimeinterval

函数优化类 防抖 节流

// 防抖
 function debounce(fun,time){
     let timer
     return function(){
       if(timer){
         clearTimeout(timer)
        }
         let argus = argument
         timer = setTimeout(() =>{
             fun.apply(this.argus)
         })
     }
   
 }
// 节流
function throttle(fun,time){
    let t1 = 0
    return function(){
        let t2 = new Date()
        if(t2- t1 > time){
            fun.apply(this,arguments)
            t1 = t2
        }
    }
}