封装代码
const orginalProto = Array.prototype;
let arrayProto = Object.create(orginalProto);
const methodsToPatch = [
'push',
'pop',
'shift',
'unshift',
'splice',
'sort',
'reverse'
];
methodsToPatch.forEach(method => {
arrayProto[method] = function () {
for(let i=0;i<arguments.length;i++){
let obj = {}
for (x in arguments[i]) {
Object.defineProperty(obj,'__'+x,{
get: function(){
return Number(this[x])*100+'%';
},
set: function(val){
this[x] = val
}
});
obj[x] = arguments[i][x];
}
arguments[i] = obj;
}
orginalProto[method].apply(this, arguments);
}
});
对其赋值
arrayProto.push({rota:0.1},{rota:0.2},{rota:0.4},{rota:0.5},{rota:0.6},{rota:0.8},{rota:0.9});
对其取值
arrayProto[0].__rota
arrayProto[0].rota
|