Comments (14)
const proxyArray = arr => {
const length = arr.length;
return new Proxy(arr, {
get(target, key) {
key = +key;
while (key < 0) {
key += length;
}
return target[key];
}
})
};
var a = proxyArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
console.log(a[1]); // 2
console.log(a[-10]); // 9
console.log(a[-20]); // 8
from fe-interview.
var arr = [1,2,[3,[4,[5]]]]
function proxyArr(arr) {
for(let i = 0; i < arr.length; i++) {
let item = arr[i];
if(Array.isArray(item)) arr[i] = proxyArr(item)
}
return new Proxy(arr,{
get(arr, index, proxiedArr){
let len = arr.length
index = +index
index = index < 0 ? index + len : index
return arr[index]
}
})
}
var proxied = proxyArr(arr)
console.log(proxied[-1][-1][-1][-1])
from fe-interview.
const negativeArray = els =>
new Proxy(els, {
get: (target, propKey, receiver) =>
Reflect.get(
target,
+propKey < 0 ? String(target.length + +propKey) : propKey,
receiver
)
});
const unicorn = negativeArray(["京", "程", "一", "灯"]);
unicorn[-1];
from fe-interview.
楼上的成倍数索引有问题。
from fe-interview.
"use strict";
let arr=[1,2,4,6,8,10];
function Arrchange(arr){
let proxy=new Proxy(arr,{
set(){},
get(arr,property){
if(property>=0){
return arr[property]
}else{
return arr[(arr.length+property*1)]
}
}
});
return proxy;
}
let brr=new Arrchange(arr);
console.log(brr[0]);
from fe-interview.
function ArrProxy(arr) {
return new Proxy(arr, {
get(arr, index) {
// 模拟机器取模运算
const modIndex = index % arr.length;
return arr[modIndex < 0 ? arr.length + modIndex : modIndex];
},
set(){}
})
}
from fe-interview.
// 30.手写用 ES6proxy 如何实现 arr[-1] 的访问
function PythonArray(arr) {
return new Proxy(arr, {
get(target, prop, receiver) {
if ((key = Number.parseInt(prop))) {
const len = Reflect.get(target, 'length', receiver);
prop = key < 0 && Math.abs(key) <= len ? len + key : prop;
}
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
if ((key = Number.parseInt(prop))) {
const len = Reflect.get(target, 'length', receiver);
prop = key < 0 && Math.abs(key) <= len ? len + key : prop;
}
return Reflect.set(target, prop, value, receiver);
},
deleteProperty(target, prop) {
if ((key = Number.parseInt(prop))) {
const len = target.length;
prop = key < 0 && Math.abs(key) <= len ? len + key : prop;
}
return Reflect.deleteProperty(target, prop);
},
});
}
from fe-interview.
function proxyArray(arr){
return new Proxy(arr, {
get(target, key){
if(key >= 0) return target[key]
let len = target.length
return target[len + (key % len)]
}
})
}
var arr = proxyArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
console.log(arr[0], arr[1])
console.log(arr[-10], arr[-20])
from fe-interview.
const newArrayProducer = (arr) =>
new Proxy(arr, {
get: function (obj, prop) {
console.log('%c prop = %s', 'color: #408059', prop);
prop = prop >= 0 ? prop : arr.length - Math.abs(prop);
return obj[prop];
},
});
var newArr = newArrayProducer([1, 2, 3]);
from fe-interview.
const genNegativeIndexAccessibleArray = <T>(arr: T[]) => {
return new Proxy(arr, {
get(target, key) {
if (typeof key !== 'symbol') {
const index = Number.parseInt(key, 10);
if (index < 0) {
const computedIndex = target.length + index;
return Reflect.get(target, computedIndex);
}
}
return Reflect.get(target, key);
},
});
};
const a = genNegativeIndexAccessibleArray([
1, 2, 3, 4, 5, 6, 7, 8, 9,
]);
console.log(a[1]); // 2
console.log(a[-1]); // 9
console.log(a[-2]); // 8
from fe-interview.
proxy的介绍proxy
利用proxy重写[]
const negativeIndex = arr => {
return new Proxy(arr, {
get: (target, proKey) => {
let index = Number(proKey);
let length = target.length;
while (index < 0) {
index += length;
}
return target[index];
}
})
}
console.log(negativeIndex([1, 2, 3, 4, 5, 6, 7])[-1]);
console.log(negativeIndex([1, 2, 3, 4, 5, 6, 7])[-20]);
from fe-interview.
不判断数字索引,length push访问是有问题的。
from fe-interview.
const proxyArr = arr => new Proxy(arr, {
get(target, key, proxy) {
let index = (target.length + +key) % target.length;
return target[+index];
},
set(target, key, value, proxy) {
},
});
// 测试
const arr = [2, 34, 65, 7, 4, 56, 78, 5, 2, 8, 0, 5, 98, 435];
console.log(proxyArr(arr)[-1]);
from fe-interview.
const negativeArr = (ele) => {
return new Proxy(ele, {
get(target, key, receiver) {
if (Array.isArray(ele)) {
const len = target.length
const intKey = Number(key)
return Reflect.get(target, intKey < 0 ? len + intKey : intKey, receiver)
}
return Reflect.get(target, key, receiver)
}
})
}
const test = negativeArr([1, 2, 3, 4, 5])
console.log(test[-1])
console.log(test[-2])
const obj = negativeArr({'-1': 1, '-2': 2})
console.log(obj[-1])
console.log(obj[-2])
from fe-interview.
Related Issues (20)
- Day378:说一下什么是 Http 协议无状态?怎么解决 Http 协议无状态?
- Day379:如果让你来实现一个前端监控系统,应该考虑什么?如何去实现?
- Day380:实现一个系统,统计前端页面性能、页面 JS 报错、用户操作行为、PV/UV、用户设备等消息,并进行必要的监控报警。方案如何设计,用什么技术点,什么样的系统架构,难点会在哪里? HOT 3
- Day381:说一下 JavaScript 严格模式下有哪些不同?
- Day382:说一下 setTimeout 和 setInterval 的区别,包含内存方面的分析?
- Day383:说下 React 的 useEffect、useCallback、useMemo HOT 2
- Day384:JavaScript 中如何实现一个类?怎么实例化这个类? HOT 2
- Day385:在一个 DOM 上同时绑定两个点击事件:一个用捕获,一个用冒泡。事件会执行几次?先执行冒泡还是捕获? HOT 9
- Day386:写一个通用的事件侦听器函数 HOT 3
- 定时函数 HOT 1
- 小程序不维护了吗? HOT 10
- --
- 小程序里面的内容还有其他地方能学习吗
- 求小程序哇!!不然学习效率太低了 HOT 1
- 现在还能在哪里看题目答案吗?小程序关闭了😭😭 HOT 1
- #### 1)为什么会有Event Loop
- > > > @Genzhen 请教一下大佬,如果在render里面结构赋值也会影响性能吗?比如 const { page, size } = this.state这种的? HOT 1
- 考察原型链
- issue路径错误,提不了pr,可见下图
- vue中&nextTick的原理
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-interview.