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

最新文章

  1. WebStorm
  2. C# 反转字符串
  3. iOS——Core Animation 知识摘抄(一)
  4. JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)
  5. HandlerMethodArgumentResolver数据绑定无效
  6. AWE、加载计数器错误
  7. Vijos P1061 迎春舞会之三人组舞 DP
  8. java集合使用——HashMap
  9. Asp.net mvc 知多少(十)
  10. Hello ReactJS
  11. 学习之-ASP.NET MVC Filter
  12. SpringBoot下配置FreeMarker配置远程模版
  13. Docker笔记三:基于LVS DR模式构建WEB服务集群
  14. centos7下NFS使用与配置
  15. undefined 与 xx is not defined 的区别
  16. vue 中 使用百度编辑器 UEditor
  17. maven项目eclipse目录结构浅析
  18. 【转】Centos yum 换源
  19. 教你在Android手机上使用全局代理
  20. hdoj1069 Monkey and Banana(DP--LIS)

热门文章

  1. [原创]react-vio-form 快速构建React表单应用
  2. CISP/CISA 每日一题 九(2017-11-30 09:25)
  3. C#中数组与ArrayList的简单使用
  4. golang sort
  5. 认识一下JavaScript
  6. 5. Node基础编程
  7. Stable Matching (Gale Sharpley Algorithm)
  8. vuepress折腾记
  9. JS中的发布订阅模式
  10. LA 3989 - Ladies' Choice 稳定婚姻问题