Summary of JS knowledge points (two)

Summary of JS knowledge points

1. What is document flow?

Document flow refers to the elements in the layout process, the elements will be arranged in order from left to right, top to bottom.
Block-level elements occupies one line, and the elements in the line are rendered in order of content size. When one line is full, the rendering starts from the next line.

Implement Reduce

function myReduce(arr, callback, initialValue) {
    
    
    if (!(arr instanceof Array)) {
    
    
        throw TypeError(`${
      
      arr} is not an array!`);
    }
    if (typeof callback !== "function") {
    
    
        throw TypeError(`${
      
      callback} is not a function!`);
    }

    // 1.遍历arr调用callback,若initialValue为undefined则赋值为第一个值
    const n = arr.length;
    if (n < 1) {
    
    
        return null;
    }

    // 2.维护一个累加值接受callback的返回值
    let accumulator;
    let startIndex = 0;
    // 若有初始值
    if (initialValue !== undefined) {
    
    
        accumulator = initialValue;
    } else {
    
    
        accumulator = arr[0];
        startIndex = 1;
    }
    for (let i = startIndex; i < n; i++) {
    
    
        accumulator = callback(accumulator, arr[i], i, arr);
    }
    // 3.返回累加值
    return accumulator;
}

Two, the difference between offsetTop and scrollTop

offsetTop: current element to the top and to the top of the parent element distance between
herein refers parent element: the offsetParent
the offsetParent: latest (the most recently comprising means hierarchy) containing the element positioning element or the nearest table, td, th, body element.
scrollTop: the current element to the top and the top of the window distance

Guess you like

Origin blog.csdn.net/wdhxs/article/details/113524500