This is the 7th day of my participation in the August Text Challenge.More challenges in August

Review some of the code from the previous article

  • Stabilization:The last timeTriggering eventN SECWithin theNot retriggeredTo execute the program, otherwiseAgain the timing
  • Throttling:N SECOnly one event is emitted within
  • Simple judgment type:typeofUnable to judge properlynullThe reference type can only be correctly determinedFunction.instancofOnly the reference type can be determined normally. The best method is:Object.prototype.toString.call(arg)
  • Shallow copy: A full copy of all attributes of an object. The copied base data type is not affected by the modification of the attributes of the copied object, while the copied reference data type is affected by the modification of the attributes of the copied object
  • Deep copy: Copies all attributes of an object completely. A new memory space is created for the referenced data type after the copy. Therefore, the modification of the attributes of the copied object is not affected
/ / image stabilization
function debounce(func,wait){
    let timer = null;
    return function(){
        if(! timer)clearTimeout(timer);
        timer = setTimeout(() = >{
            func.call(this.arguments)
        },wait)
    }
}
/ / throttling
function throttle(func,delay){
    let canRun = true
    return function(){
        if(! canRun)return;
        canRun = false;
        setTimeout(() = >{
            func.call(this.arguments);
            canRun = true;
        },delay)
    }
}
/ / throttling new
function throttle(func,wait){
    let pre = 0;
    return function(){
        let now = Date.now();
        if(now-pre>wait){
            func.call(this.arguments) pre = now; }}}// Simply determine the type
function getType(arg){
    return Object.prototype.toString.call(arg).slice(8, -1).toLowerCase();
}
Copy / * * /
var person = {
    "name":"tony"."age":21."tech": {"react":false}}/ / shallow copy
let personCopy1 = Object.assign({},person)  / / Object. The assign method
letpersonCopy2 = {... person}/ / ES6 expansion method
/ / copy
let personCopy3 = JSON.parse(JSON.stringify(person)) / / JSON parse (JSON. Stringify (obj)) method
// Recursive deep copy method
function deepClone(obj){
    if(typeof(obj) ! = ='object') return;
    var newObj = obj instanceof Array? [] : {};for(var key in obj){
        if(obj.hasOwnProperty(key)){
            newObj[key] = typeof obj[key] === 'object'? deepClone(obj[key]) : obj[key]; }}return newObj;
}
Copy the code

Chapter TWO

1. The inheritance

There are two common inheritance methods:

  • Combinatorial inheritance (stereotype chain inheritance + constructor inheritance)
  • Parasitic combinatorial inheritance (an improvement on combinatorial inheritance,)
/* Method 1: combinatorial inheritance */
function Parent(age){
    this.age = age;
}
Parent.prototype.sayAge = function(){
    console.log(this.age);
}
// the constructor is used to set the variable
function Child(age){
    Parent.call(this,age)
}
// Modify the prototype, inherit the method
Child.prototype = new Parent();
////////////////////////////////////////////////////////////
/* Parasitic combinatorial inheritance (an improvement on combinatorial inheritance) */
function Parent(age){
    this.age = age;
}
Parent.prototype.sayAge = function(){
    console.log(this.age);
}
// the constructor is used to set the variable
function Child(age){
    Parent.call(this,age);
}
// Prototype chain modification
const prototype = object(Parent.prototype);
prototype.constructor = Child;
Child.prototype = prototype;
Copy the code

2. Array deduplication

Methods:

  • ES5 filter
  • ES6 Set
//ES5 filter
function unique(arr){
    var res = arr.filter(function(item,index,array){
        return array.indexOf(item) === index
    })
    return res;
}
//ES6 Set
var unique = arr= > [...new Set(arr)]
Copy the code

3. Array flattening

Array flattening: Flatten a multidimensional array into a one-dimensional array

Methods:

  • recursive
  • ES6 array.some
/ / ES5 recursion
function flattern(arr){
    var array = [];
    for(var i=0; i<arr.length; i++){if(arr[i].isArray)  array = array.concat( flattern(arr[i]) )
        else array.push(arr[i])
    }
    return array
}
//ES6
function flatten(arr) {
    while (arr.some(item= > Array.isArray(item))) { arr = [].concat(... arr); }return arr;
}
Copy the code

4.new

Steps:

  • Creating an empty object
  • Point to the object prototype
  • Modify the empty object context
  • Return empty object
function newFunc(Constructor,... args){
    var obj = {}
    obj.__proto__ = Constructor.prototype
    Constructor.call(obj,args)
    return obj
}
Copy the code