js最佳实践方式(2023/3/29)

62 阅读4分钟

创建一个数组它的每一项是对应参数再加上前面的参数的总和

  • 1.使用Array.prototype.reduce(),初始化一个空数组用它迭代累加'nums'的每一项的值
  • 2.使用Array.prototype.slice(),获取之前的部分总和或'0'值,并将当前计算总和添加到其数组中。
  • 3.使用扩展运算符(…)将新的部分和添加到包含先前和的累加器数组中。
const accumulate = (...nums) =>
  nums.reduce((acc, n) => [...acc, n + (acc.slice(-1)[0] || 0)], []);
accumulate(1234); // [1, 3, 6, 10]
accumulate(...[1234]); // [1, 3, 6, 10]

知识点:

  • Array.prototype.slice() slice(begin, end) 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。

    • begin负数,则表示从原数组中的倒数第几个元素开始提取。
    • end表示在原数组中的倒数第几个元素结束抽取
  • Array.prototype.reduce() reduce((previousValue, currentValue)=>callbackFn, initialValue)。

    • previousValue上一次调用 callbackFn 时的返回值。在第一次调用时,若指定了初始值 initialValue,其值则为 initialValue,否则为数组索引为 0 的元素 array[0]。

    • currentValue数组中正在处理的元素。在第一次调用时,若指定了初始值 initialValue,其值则为数组索引为 0 的元素 array[0],否则为 array[1]。

    • initialValue作为第一次调用 callback 函数时参数 previousValue 的值。若指定了初始值 initialValue,则 currentValue 则将使用数组第一个元素;否则 previousValue 将使用数组第一个元素,而 currentValue 将使用数组第二个元素。  

HTML元素添加一个类名

  • 1.使用Element.classList的add()方法,将指定的类添加到元素中。
const addClass = (el, className) => el.classList.add(className);
addClass(document.querySelector('p'), 'special');
// 这个p节点现在有了“special”类

知识点:

  • Element.classListElement.classList 是一个只读属性,返回一个元素 class 属性的动态 DOMTokenList 集合。这可以用于操作 class 集合。

    • classList.add( newClassName ):添加新的类名,如已经存在,取消添加

    • classList.contains( oldClassName ):确定元素中是否包含指定的类名,返回值为true 、false;

    • classList.remove( oldClassName ):移除已经存在的类名;

    • classList.toggle( className ):如果classList中存在给定的值,删除它,否则,添加它;

    • classList.replace( oldClassName,newClassName ):类名替换

计算给定日期后n天的日期,返回其字符串表示形式。

  • 1.使用第一字符串date时间参数值,创建一个时间对象。
  • 2.使用Date.prototype.getdate()Date.prototype.setdate()将给定日期加上n天。
  • 3.使用Date.prototype.toISOString()返回一个'YYYY-MM-DDTHH:mm:ss.sssZ'格式的字符串。用split处理成需要的类型。
const addDaysToDate = (date, n) => {
  const d = new Date(date);
  d.setDate(d.getDate() + n);
  return d.toISOString().split('T')[0];
};
addDaysToDate('2020-10-15'10); // '2020-10-25'
addDaysToDate('2020-10-15', -10); // '2020-10-05'

知识点:

  • Date.prototype.setDate()setDate(dayValue) 方法根据本地时间来指定一个日期对象的天数。

    • dayValue一个整数,表示该月的第几天。如果 dayValue 超出了月份的合理范围,setDate 将会相应地更新 Date 对象。例如,如果为 dayValue 指定 0,那么日期就会被设置为上个月的最后一天。如果是3月的32那么日期就会被设置为上个月的最后一天
  • Date.prototype.getDate(): 返回日期对象年月日中的日。

计算距给定日期第n分钟的日期,并返回其字符串表示形式。

  • 第一个参数使用Date构造函数创建Date对象。
  • 使用 Date.prototype.getTime() 和 Date.prototype.setTime() 给指定日期添加n分钟。
  • 使用 Date.prototype.toISOString()String.prototype.split() 和 String.prototype.replace() 以yyyy-mm-dd HH:mm:SS格式返回字符串。
const addMinutesToDate = (date, n) => {
  const d = new Date(date);
  d.setTime(d.getTime() + n * 60000);
  return d.toISOString().split('.')[0].replace('T',' ');
};
addMinutesToDate('2020-10-19 12:00:00'10); // '2020-10-19 12:10:00'
addMinutesToDate('2020-10-19', -10); // '2020-10-18 23:50:00'

知识点:

  • 和前面的getData和setData一样不过是换成了setTime和getTime

将事件侦听器附加到所有提供的目标元素上面。

  • 使用 Array.prototype.forEach() 和 EventTarget.addEventListener() 将事件类型和提供的监听函数全部附加到所有的目标元素上面。
const addEventListenerAll = (targets, type, listener, options, useCapture) => {
  targets.forEach(target =>
    target.addEventListener(type, listener, options, useCapture)
  );
};
addEventListenerAll(document.querySelectorAll('a'), 'click'() =>
  console.log('Clicked a link')
);
// Logs 'Clicked a link' whenever any anchor element is clicked