DemuMesDataV/src/plugins/methodsExtend.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-12-05 13:49:32 +08:00
const { parse, stringify } = JSON
export function deepClone (object) {
return parse(stringify(object))
}
export function deleteArrayAllItems (arrays) {
arrays.forEach(element => element.splice(0, element.length))
}
export function debounce (delay, callback) {
let lastTime
return function () {
clearTimeout(lastTime)
const [that, args] = [this, arguments]
lastTime = setTimeout(() => {
callback.apply(that, args)
}, delay)
}
}
2018-12-09 19:49:04 +08:00
export function randomExtend (minNum, maxNum) {
if (arguments.length === 1) {
return parseInt(Math.random() * minNum + 1, 10)
} else {
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
}
}
2018-12-07 15:50:45 +08:00
export function multipleSum (...num) {
let sum = 0
num.forEach(n => (sum += n))
return sum
}
2018-12-05 13:49:32 +08:00
export default function (Vue) {
Vue.prototype.deepClone = deepClone
Vue.prototype.deleteArrayAllItems = deleteArrayAllItems
Vue.prototype.debounce = debounce
2018-12-07 15:50:45 +08:00
Vue.prototype.multipleSum = multipleSum
2018-12-09 19:49:04 +08:00
Vue.prototype.randomExtend = randomExtend
2018-12-05 13:49:32 +08:00
}