[Javascript] Customize Behavior when Accessing Properties with Proxy Handlers

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Javascript] Customize Behavior when Accessing Properties with Proxy Handlers相关的知识,希望对你有一定的参考价值。

Proxy allows you to trap what happens when you try to get a property value off of an object and do some behavior before the value is accessed. For example, you could check the name of the property and always return a certain value or even check if the property is undefined and return some default. The options are unlimited.

 

"use strict"

let person = 
  name: "John"


let handler = 
  get(target, key) 
    if (key === "name") 
      return "Mindy"
    

    if (Reflect.has(target, key)) 
      return Reflect.get(target, key)
    

    return "You tried to access something undefined"
  


person = new Proxy(person, handler)

console.log(person.name) // "Mindy"
console.log(person.age) // "You tried to access something undefined"

  

以上是关于[Javascript] Customize Behavior when Accessing Properties with Proxy Handlers的主要内容,如果未能解决你的问题,请参考以下文章

javascript 追踪 - BE / FE

javascript 追踪 - BE

antd.customize.less 字体系列的麻烦(请帮助)

如何将 React-app-rewired 与 Customize-CRA 集成

text CUSTOMIZE PLACEHOLDER

css customize.css