深拷贝
const deepCloneObj = JSON.parse(JSON.stringify(obj));
function deepClone(obj, cache = new WeakMap()) {
if (obj === null || typeof obj !== "object") return obj;
if (obj instanceof Date) return new Date(obj);
if (obj instanceof RegExp) return new RegExp(obj);
if (cache.has(obj)) return cache.get(obj);
let cloneObj = new obj.constructor();
cache.set(obj, cloneObj);
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
cloneObj[key] = deepClone(obj[key], cache);
}
}
return cloneObj;
}
const obj = { name: "Jack", address: { x: 100, y: 200 } };
obj.a = obj;
const newObj = deepClone(obj);
console.log(newObj.address === obj.address);
判断是否是一个数组
const arr = [];
console.log(
Array.isArray([]),
Array.isArray(arr),
Array.isArray([1]),
Array.isArray(new Array()),
Array.isArray(new Array(1, 2, 3, 4, 5)),
Array.isArray(Array.prototype)
);
console.log(arr.constructor === Array);
console.log(
Array.prototype instanceof Object,
[] instanceof Array,
arr instanceof Array
);
console.log(Array.prototype.isPrototypeOf(arr));
console.log(
Object.prototype.toString.call(arr),
Object.prototype.toString.call(arr) === "[object Array]"
);
reduce
reduce 实现 map
Array.prototype.myMap = function (callback) {
if (typeof callback !== "function")
throw new Error("callback must be a function!");
return this.reduce((pre, val, ind, arr) => {
pre.push(callback(val, ind, arr));
return pre;
}, []);
};
const nums = [1, 5, 6].myMap((v) => v + 1);
console.log(nums);
reduce 实现 filter
Array.prototype.myFilter = function (callback) {
if (typeof callback !== "function")
throw new Error("callback must be a function!");
return this.reduce((pre, val, ind, arr) => {
if (callback(val, ind, arr)) pre.push(val);
return pre;
}, []);
};
const nums2 = [1, 5, 6].myFilter((v) => v > 2);
console.log(nums2);
reduce 实现数组去重
let arr = [1, 2, 3, 1, 1, 2, 3, 3, 4, 3, 4, 5];
const res = arr.reduce((pre, val, ind, arr) => {
if (!pre.includes(val)) pre.push(val);
return pre;
}, []);
console.log(res);
reduce 实现数组扁平化
let arr2 = [
1,
2,
"3js",
[4, 5, [6], [7, 8, [9, 10, 11], null, "abc"], { age: 58 }, [13, 14]],
"[]",
null,
];
function myFlat(arr) {
if (!Array.isArray(arr)) throw new Error(`${arr} must be array`);
return arr.reduce((pre, val, ind, arr) => {
return Array.isArray(val) ? [...pre, ...myFlat(val)] : [...pre, val];
}, []);
}
console.log(myFlat(arr2));
防抖
function debounce(fn, delay) {
let timer = null;
return () => {
let args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
节流
function throttle(fn, delay = 1000) {
var prev = null;
console.log(prev);
return function () {
var args = arguments;
var now = Date.now();
if (now - prev >= delay) {
fn.apply(this, args);
prev = now;
}
};
}
JS 判断当前运行环境是手机还是电脑
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
alert("您是手机登录");
} else {
alert("您是电脑登录");
}