directory

  1. Function default argumentswitharguments
  2. An asynchronous functionIn accordance with thesequential
  3. Implement a React componentmodal.show()Way to call
  4. Subscript lookup of an array elementwithTime complexity
  5. new WebSocketWhat happened?
  6. The wsThe heartbeat checkHow is it done
  7. Ws is established through aHTTP request, andSet up process
  8. Ws introduced
  9. Binary treebeNodes illuminated on the rightTo obtain

To do:

  1. Binary trees (nodes are all one in, one out),Identify problems with circular referencesGets a method for specifying nodes

Function default arguments and arguments

  • JavaScript delves into things like array objects and arguments

Function default arguments and arguments affect the result of code execution.

1) Demo1 has default parameters

Compiling Babel through ES6 differs from compiling ES5, resulting in different code output. If there are default parameters, compile as ES6, if there are no, compile as ES5.

Arguments cannot be changed in ES6?

function side(arr) {
    console.log(arr[0]);
    arr[0] = arr[2];
}
function a(a, b, c = 3) {
    c = 10;
    side(arguments);
    return a + b + c;
}

console.log(a(1.2.3));
Copy the code

1) Demo2 has no default parameters

function side(arr) {
    arr[0] = arr[2];
}
function a(a, b, c) {
    c = 10;
    side(arguments);
    return a + b + c;
}

console.log(a(1.2.3));
Copy the code

Asynchronous functions are executed sequentially

Enter an array of asynchronous functions that are executed in order

The following demo is just an implementation idea and is not available

function pipePromise(arr) {
    if (!Array.isArray(arr)) {
        return;
    }
    for (let i = 0; i <= arr.length; i++) {
        // arr[i]()
        // The fn asynchronous function is put into the task queue and executed after completion, Fn1 is executed
        // How to determine the completion of asynchronous function
        let promiseI = new Promise((res, rej) = > {
            res(arr[i]());
        });
        if (promiseI.status === 'fullied' || promiseI.status === 'rejected') {}}}Copy the code
function pipePromise(arr) {
    if (!Array.isArray(arr)) {
        return;
    }

    let arr1 = arr.reduce((pre, cur) = > {
        // if(cur()) {
        // pre.push(cur)
        // }
        pre.then(cur);
    }, Promise.resolve);
}
Copy the code

Implement a React component, which can be called modal.show()

function Modal() {
    const [] = useState();
    this.show = () = > {
        document.appendChild(domFN);
    };

    render(<>domFN</>.'id1');
}

// call
let modal = new Modal();
modal.show();
Copy the code

Subscript search and time complexity of array elements

An input array is an array of randomly selected items, segmented, and swapped places such as [1.2.3.4.5], the randomly selected position is2, the data after segmentation is [3.4.5.1.2The method to implement is from [3.4.5.1.2] to find the subscript of any element, and give the optimal time complexity solution. For example, in a loop, the time complexity is order n.Copy the code

There are other ways to do this without loops

Five, what happened to New WebSocket?

  • Juejin. Cn/post / 684490…

Sixth, the wsThe heartbeat checkHow is it done

  • Front-end webSocket realizes chat message & heartbeat detection & disconnection reconnection

Seven, the wsSet up process

– This indicates converting HTTP to WebSocket

  • This section describes how to upgrade HTTP and Websocket

In the previous business, it was the business that needed to send an HTTP request connection before WS was established.

Setting up WS does not require sending HTTP requests in advance

8. Introduction to WS

  • Juejin. Cn/post / 684490…

Nine,Binary treebeNodes illuminated on the rightTo obtain

Gets the nodes in the diagram that are illuminated to the right

1) The following code is only an idea and cannot be used

The following ideas are not good, and will be supplemented later

Divide it into left, right, and determine the hierarchy of the left and right branches. If the left side is deep, then take out the node on the right side of the left side. Then, take out all the nodes on the right side of the right branch.

// type Node = { left? : Node; right? : Node; value: any };
let treeObj = {
    'a': {'a1': {'a11': {
                aValue: 'aValue'}},'a2': {'a21': 'a21 value'.'a22': 'a22 value'}}};const isObjFn = (o) = > {
    return Object.prototype.toString.call(o) === '[object Object]'
};

const deepFn = (treeObj) = > {
    if(! isObjFn(treeObj)){return; }let leftIndex=0; // Left tree level
    let rightIndex=0; // Right tree level
    let right = [];// Store the node on the right
    let left = []; // Store the left node
    let lightNode = []; // Store parallel light nodes

    for(let i in treeObj){
        if( isObjFn(treeObj[i]) ){
            deepFn(treeObj[i])
        }else{
            // Storage node
            if(i==='a1') { // Left root node

            }else if(i==='a2') {// Right root node

            }else{
                void null}}}};Copy the code

Repeat string

1) the demo

1.1) While
let str = 'abc';
function repeat(str=' ', num=0){
    if(typeofstr ! ='string') {return};
    // optimize to change the number of times while is executed
    // If num = num/2, then STR + STR

    O(logA) + O(logb) + O(logc
    let resStr = ' ';
    while(num--){
       resStr += str; 
    }
    return resStr;
}
repeat(str,3)
// "abcabcabc"
Copy the code
1.2) Recursive approach

The script has a problem and cannot be used. Change to O(loga) + O(logb) + O(logc) time complexity

let str = 'abc';
function repeat(str=' ', num=0){
    if(typeofstr ! ='string') {return};
    O(logA) + O(logb) + O(logc
    let resStr = ' ';
    if(num%2= = =0) {/ / even
        resStr += str; 
        repeat(str, num11 | num/2) // The number of recursions is num11
    }
    
 
    return resStr + resStr;
}
repeat(str,3); // If there is a problem, follow up

Copy the code

2) Rule search

// O(loga) + O(logb) + O(logc)
// 1 * 2^0
// 0 * 2^0 + 1 * 2^1
// 3 // 1 * 2^0 + 1 * 2^1
// 4/0 * 2^0 + 0 * 2^1 + 1 * 2^2
// 1 * 2^0 + 0 * 2^1 + 1 * 2^2
// 0 * 2^0 + 1 * 2^1 + 1 * 2^2

// Rule find binary rule
// 1 // 1
// 2 // 0 1
// 3 // 1 1
// 4 // 0 0 1
// 5 // 1 0 1
// 6 // 0  1  1
Copy the code

The binary rule, if the binary number of 6 is 110 then 6 is represented by 1 in the first place to the power of 2 and 1 in the second place to the power of 1, and 0 in the third place to the power of 0

The following

0 * 2^0 + 1 * 2^1 + 1 * 2^2

Copy the code

other

  • AST syntax treeWhen parsingtoken lexeme
  • 5 times 10 to the 1 plus 6 times 10 to the 0// 15 is base 10. 56 means 5 to the first power of 10 +6 to the zero power of 10

conclusion