自用

防抖

let debounce = (fn,wait)=>{
    let timer = 0;
    return (...args)=>{
        if(timer)clearTimeout(timer);
        timer = setTimeout(()=>{
            fn.apply(this,args)
        },wait)
    }
}
复制代码

节流

const throttle = (fn, wait) => {
    let isExecuted = true;
    return (...args) => {
        if(!isExecuted)return;
        isExecuted = false;
        setTimeout(() => {
            fn.apply(this, args);
            isExecuted = true;
        }, wait);
    };
};
复制代码

深拷贝

// 思路:拷贝的数据类型分为:基本数据类型、
const deepClone = (target, map = new Map()) => {
    // 首先处理基本数据类型
    if((typeof target !== 'object' ||  target !== 'function') || target !== null) return target;
    // 判断具体的引用数据类型
    let type = Object.prototype.toString.call(target);
    let cloneTarget;

}
复制代码

ES6新增的东西

// let和const:var 存在变量提升和会绑定到全局window对象

// 解构赋值 :数组结构和对象结构

// 箭头函数 特点:形参只有一个可以省略小括号、函数体只有一句话,可以省略大括号,写在一行、不用写return、没有 arguments、this为外部作用域的this、不能作为构造函数

// 扩展运算符

// 新增数组方法
find() 和 findIndex()
// find返回找出第一个符合条件的数组成员,否则返回undefined。[1, 4, -5, 10].find((n) => n < 0)  -5
// findIndex返回找到第一个符合条件的数组成员索引值,不存在则返回-1。[1, 5, 10, 15].findIndex((value)=>  value > 9) 2
复制代码

www.jianshu.com/p/0120580f3…

blog.csdn.net/weixin_4493…

www.cnblogs.com/wasbg/p/111…

闭包实现模块化

实现链式操作

函数想要实现链式调用就必须返回一个实例,比如说数组map之后可以继续调用…

//创建一个类
class Person{
  setName(){
    this.name = name;
    return this;
  }
  setAge(){
    this.age = age;
    return this;
  }
};
//实例化
var person= new Person();
person.setName("Mary").setAge(20);

链式操作的缺点:比如说我原型上的某个方法想返回某个值,就不能再链式调用下去,或者只能放到最后去调用。

链式操作的有点:可以使得异步编程的流程更加清晰,不会像回调函数一样相互耦合,
jQuery和ES6中的Promise也正是沿用了这一思想,Promise每一个异步任务返回一个Promise对象,
通过then方法指定回调函数。
复制代码

属性的.和[]

this.xxx 和 this['xxx' + a] 是一样的

一开始JS引擎只有this['xxx'],现在写this.xxx其实JS引擎也会内部转成this['xxx']
复制代码

常见的运算符

算术运算符

加减乘除取余、自增、自减
复制代码

赋值运算符

=、+=、-=、*=、/=、%=、按位与赋值(&=)、按位或赋值(|=)、按位非赋值(~=)、
复制代码

位运算符

按位与(&)、按位或(|)、按位非(~)、按位异或(^)、<<左移、>>右移、>>>有符号右移

二进制:512 256 64 32 | 16 8 4 2 1(左右手)
负数:将正数10互换再加上1   -9 =>1001=>0110+1=> 1111 1111 1111 1111 1111 1111 1111 0111

按位非(NOT)
~25 => -(25)-1  => -26
~~25 => -(25)-1  => -26 => -(-26)-1 => 25

按位与(AND)
25 & 3 => 11001 & 00011 => 找同时存在1的位数,才取1 => 00001 => 1

按位或(OR)
25 | 3 => 11001 & 00011 => 找存在1的位数,就取1 => 11011 => 27

按位异或(XOR)
25 ^ 3 => 11001 & 00011 => 找存在1的位数,就取1,同时存在,则不取 => 11010 => 26
var a=10,b=9; a ^= b, b ^= a, a ^= b; //a=9,b=10


<<左移
-2<<5 => 2的二进制为10,全部向做移5位 => 0000010-> 1000000 => -64

>>右移
该操作符会将第一个操作数向右移动指定的位数。向右被移出的位被丢弃
-9 >> 2 => 1001 -> 10 => -2

>>>有符号右移
正数>>>和>>结果一样,负数>>>数值会很大

取整:
~~3.9 => 3
3.9 | 0 => 3
3.9 ^ 0 => 3
3.9 << 0 => 3
复制代码

需要注意的关键字

in

1.配合for遍历对象。
2.判断某个属性属于某个对象(包括原型链上的原型)
如:
"make" in {make: "Honda", model: "Accord", year: 1998}  // returns true
0 in ["redwood", "bay", "cedar", "oak", "maple"]        // returns true
"length" in trees // returns true (length is an Array property)
复制代码

instanceof

一个对象能否通过原型链找到另一个对象的原型

www.runoob.com/jsref/jsref…

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享