1.阻止冒泡

  1. export const stopPropagation = (e) => {
  2. e = e || window.event;
  3. if(e.stopPropagation) { // W3C阻止冒泡方法
  4. e.stopPropagation();
  5. } else {
  6. e.cancelBubble = true; // IE阻止冒泡方法
  7. }
  8. }

2.防抖函数

  1. export const debounce = (fn, wait) => {
  2. let timer = null;
  3. return function() {
  4. let context = this,
  5. args = arguments;
  6. if (timer) {
  7. clearTimeout(timer);
  8. timer = null;
  9. }
  10. timer = setTimeout(() => {
  11. fn.apply(context, args);
  12. }, wait);
  13. };
  14. }

3.节流函数

  1. export const throttle = (fn, delay) => {
  2. let curTime = Date.now();
  3. return function() {
  4. let context = this,
  5. args = arguments,
  6. nowTime = Date.now();
  7. if (nowTime - curTime >= delay) {
  8. curTime = Date.now();
  9. return fn.apply(context, args);
  10. }
  11. };
  12. }

4.数据类型判断

  1. export const getType = (value) => {
  2. if (value === null) {
  3. return value + "";
  4. }
  5. // 判断数据是引用类型的情况
  6. if (typeof value === "object") {
  7. let valueClass = Object.prototype.toString.call(value),
  8. type = valueClass.split(" ")[1].split("");
  9. type.pop();
  10. return type.join("").toLowerCase();
  11. } else {
  12. // 判断数据是基本数据类型的情况和函数的情况
  13. return typeof value;
  14. }
  15. }