JS知识点总结
一、什么是文档流?
文档流是指元素在排版布局过程中,元素会按照从左向右,从上到下的顺序排列。
块级元素占满一行,行内元素按照内容大小顺序进行渲染,当一行满了之后从下一行开始渲染。
实现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;
}
二、offsetTop和scrollTop区别
offsetTop:当前元素顶端和父元素顶端之间的距离
这里的父元素指:offsetParent
offsetParent:最近的(指包含层级上的最近)包含该元素的定位元素或者最近的 table,td,th,body元素。
scrollTop:当前元素顶端和窗口顶端的距离