vue数组方法 复制对象

在main.js写

// eslint-disable-next-line no-extend-native
Array.prototype.indexOf = function (val) {
for (let i = 0; i < this.length; i++) {
if (this[i] === val) return i
}
return -1
}
// eslint-disable-next-line no-extend-native
Array.prototype.remove = function (val) {
let index = this.indexOf(val)
if (index > -1) {
this.splice(index, 1)
}
}

复制对象
Vue.prototype.clone = function (obj) {
var copy = (obj instanceof Array) ? [] : {}
for (let attr in obj) {
if (!obj.hasOwnProperty(attr)) continue
copy[attr] = (typeof obj[attr] === 'object') ? this.clone(obj[attr]) : obj[attr]
}
return copy
}

调用
this.clone(obj)
posted @ 2020-02-26 11:36  sosolucky  阅读(2380)  评论(0编辑  收藏  举报