[Javascript] Customize Behavior when Accessing Properties with Proxy Handlers
2024-09-01 22:55:46
A 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"
最新文章
- amazon oa1 - search in 2D array II [Leetcode] 240
- 怎么样修改PHPStorm中文件修改后标签和文件名的颜色与背景色
- 25 uname-用于显示系统信息
- HTML5学习总结-08 应用缓存(Application Cache)
- Appium使用PageFactory初始化对象时报空指针错误
- PHP 5.6正式发布:新特性、及功能改进介绍
- Jasper_table_resolve get multiple copies of table in detail band issue
- javascript 工作必知(四) 类型转换
- AFNetWorking 对汉字部分UTF-8编码
- ssh密钥分发与ansible
- ECharts中color : function的用法(转)
- Nginx反向代理的基本配置
- linux基础之正则表达式
- 分布式Snowflake雪花算法
- LeetCode--No.006 ZigZag Conversion
- LINUX内核分析第七周学习总结——可执行程序的装载
- django rest framework跨表取值
- js阻止事件冒泡的两种方法
- Shell脚本中实现切换用户并执行命令操作【转】
- pymongo创建索引