This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selfMap = function (fn, context) { | |
let arr = Array.prototype.slice.call(this) | |
let mappedArr = Array() | |
for (let i = 0; i < arr.length; i++) { | |
if (!arr.hasOwnProperty(i)) continue; | |
mappedArr[i] = fn.call(context, arr[i], i, this) | |
} | |
return mappedArr | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selfFilter = function (fn, context) { | |
let arr = Array.prototype.slice.call(this) | |
let filteredArr = [] | |
for (let i = 0; i < arr.length; i++) { | |
if(!arr.hasOwnProperty(i)) continue; | |
fn.call(context, arr[i], i, this) && filteredArr.push(arr[i]) | |
} | |
return filteredArr | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selfSome = function (fn, context) { | |
let arr = Array.prototype.slice.call(this) | |
if(!arr.length) return false | |
for (let i = 0; i < arr.length; i++) { | |
if(!arr.hasOwnProperty(i)) continue; | |
let res = fn.call(context,arr[i],i,this) | |
if(res)return true | |
} | |
return false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selfReduce = function (fn, initialValue) { | |
let arr = Array.prototype.slice.call(this) | |
let res | |
let startIndex | |
if (initialValue === undefined) { | |
for (let i = 0; i < arr.length; i++) { | |
if (!arr.hasOwnProperty(i)) continue | |
startIndex = i | |
res = arr[i] | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const selfFlat = function (depth = 1) { | |
let arr = Array.prototype.slice.call(this) | |
if (depth === 0) return arr | |
return arr.reduce((pre, cur) => { | |
if (Array.isArray(cur)) { | |
return [...pre, ...selfFlat.call(cur, depth - 1)] | |
} else { | |
return [...pre, cur] | |
} | |
}, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function curry(fn) { | |
if (fn.length <= 1) return fn; | |
const generator = (...args) => { | |
if (fn.length === args.length) { | |
return fn(...args) | |
} else { | |
return (...args2) => { | |
return generator(...args, ...args2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const debounce = (func, time = 17, options = { | |
leading: true, | |
context: null | |
}) => { | |
let timer; | |
const _debounce = function (...args) { | |
if (timer) { | |
clearTimeout(timer) | |
} | |
if (options.leading && !timer) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const throttle = (func, time = 17, options = { | |
leading: true, | |
trailing: false, | |
context: null | |
}) => { | |
let previous = new Date(0).getTime() | |
let timer; | |
const _throttle = function (...args) { | |
let now = new Date().getTime(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// getBoundingClientRect | |
let imgList1 = [...document.querySelectorAll(".get_bounding_rect")] | |
let num = imgList1.length | |
let lazyLoad1 = (function () { | |
let count = 0 | |
return function () { | |
let deleteIndexList = [] | |
imgList1.forEach((img,index) => { | |
let rect = img.getBoundingClientRect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Randomly select one of all elements after the current element to exchange with the current element | |
function shuffle(arr) { | |
for (let i = 0; i < arr.length; i++) { | |
let randomIndex = i + Math.floor(Math.random() * (arr.length - i)); | |
[arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]] | |
} | |
return arr | |
} | |
// Generate a new array, randomly take an element from the original array and put it into the new array |