-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
手写用 ES6proxy 如何实现 arr[-1] 的访问(滴滴2020) #134
Comments
let arr= [1,2,3,4]
let proxy = new Proxy(arr,{
get(target,key){
if(key<0){
return target[target.length+parseInt(key)]
}
return target[key]
}
})
console.log(proxy[-2]);
console.log(proxy[2]); |
|
let arr = [1, 2, 3, 4]
let proxy = new Proxy(arr, {
get(target, key) {
if (key < 0) {
return target[target.length + parseInt(key)]
}
return target[key]
}
})
// 测试
console.log(proxy[-1]); |
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function observe(arr) {
const proxy = new Proxy(arr, {
get(target, key) {
let v = target[key];
if (typeof v === "object") {
observe(v);
}
if (key < 0) {
let len = target.length;
key = len - -key;
}
return target[key];
},
});
return proxy;
}
const arr2 = observe(arr);
console.log(arr2[-4]); |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: