zoukankan      html  css  js  c++  java
  • [ES6] Use ES6 Proxies

    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[3]); // Nevel Longbottom
    console.log(proxy[0]); // Harry Potter
    console.log(proxy[-2]); // Scabbers
  • 相关阅读:
    FIR滤波器相关解释
    FIR数字信号滤波器
    图像中的插值
    对DDS的深度认识
    嵌入式媒体处理(EMP)中的编码和解码
    FPGA噪声干扰
    视频压缩概述
    ALTERA DDRII IP核使用
    MyEclipse的使用
    Java开发API文档资源
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6275341.html
Copyright © 2011-2022 走看看