var obj = new Proxy({},{
get: function(target,propKey,receiver) {
console.log(`getting ${propKey}!`);
return Reflect.get(target,propKey,receiver);
},
set: function(target,propKey,value,receiver) {
console.log(`setting ${propKey}!`);
return Reflect.set(target,propKey,value,receiver);
}
});
// obj.count = 0;
// obj.count++;
// console.log(obj.count);
var arry = [1,2,3];
var obj2 = new Proxy(arry, {
get: function(target, propKey, receiver) {
console.log(target);
console.log(propKey);
// console.log(receiver);
// console.log(target, propKey, receiver);
if(propKey < 0) {
return target[target.length - 1];
}
return target[propKey];
}
})
console.log(obj2[-1]);
// 封装
function createArray(args) {
const handle = {
get: function(target, propKey) {
if(propKey < 0) {
return target[target.length - 1];
}
return target[propKey];
}
}
return new Proxy(args, handle);
}
console.log(new createArray([1,2,3])[-1]);
console