[ES6] Use ES6 Proxies
2024-09-03 08:55:43
A JavaScript Proxy allows you to intercept operations performed on objects, arrays, or functions like property lookup, assignment, invocation, property deletion, and more to add custom behavior. In this lesson we look at how to intercept property lookup with the get
"trap" that will allow us to get items starting from the end of the array with ease.
console.clear() const characters = [
'Harry Potter',
'Ron Weasly',
'Hermione Granger',
'Nevel Longbottom',
'Lavender Brown',
'Scabbers',
'Pigwidgeon',
] const handler = {
// target: the array itself
// name: the index which passed in
get(target, name) { // check whether index is 0,1...6
if(name in target) { // if yes, then get the value back
return Reflect.get(target, name)
} else { // if not, then the name is -1, -2, -3...
const index = Number(name);
return Reflect.get(target, target.length + index)
}
}
} const proxy = new Proxy(characters, handler) console.log(proxy[]); // Nevel Longbottom
console.log(proxy[]); // Harry Potter
console.log(proxy[-]); // Scabbers
最新文章
- WebStorm
- C# 反转字符串
- iOS——Core Animation 知识摘抄(一)
- JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)
- HandlerMethodArgumentResolver数据绑定无效
- AWE、加载计数器错误
- Vijos P1061 迎春舞会之三人组舞 DP
- java集合使用——HashMap
- Asp.net mvc 知多少(十)
- Hello ReactJS
- 学习之-ASP.NET MVC Filter
- SpringBoot下配置FreeMarker配置远程模版
- Docker笔记三:基于LVS DR模式构建WEB服务集群
- centos7下NFS使用与配置
- undefined 与 xx is not defined 的区别
- vue 中 使用百度编辑器 UEditor
- maven项目eclipse目录结构浅析
- 【转】Centos yum 换源
- 教你在Android手机上使用全局代理
- hdoj1069 Monkey and Banana(DP--LIS)