[Javascript] Reflect vs obj[key]

The Reflect namespace object contains static methods for invoking interceptable JavaScript object internal methods. The methods are the same as those of proxy handlers.

For internal methods such as [[GET]]

https://262.ecma-international.org/15.0/index.html?_gl=1*1wddf7i*_ga*MjAxMzMxOTcxMy4xNzMxNzgzMzU4*_ga_TDCK4DWEPP*MTczMTc4MzM1Ny4xLjAuMTczMTc4MzM1Ny4wLjAuMA..

Return the value of the property whose key is propertyKey from this object. If any ECMAScript code must be executed to retrieve the property value, Receiver is used as the this value when evaluating the code.

 

So for a proy handler, should we use target[key], or Reflect.get?

// using target[key]
function get(target, key, receiver) {
    return target[key]
}

// using Reflect.get
function get(target, key, receiver) {
    return Reflect.get(target, key, receiver)
}

new Proxy(target, {get})

 

Example:

const obj = {
    a: 1,
    b: 2,
    get c() {
        console.log(this)
        return this.a + this.b
    }
}

// use traget[key]
// internal method target[[GET]](key, target)
// this points to obj itself

// use Reflect.get(target, key, receiver)
// internal method you can tell the receiver
// this points to the Proxy

 

posted @   Zhentiw  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2022-11-17 [Typescript] 108. Easy - Nullable
2022-11-17 [Typescript] Variable assignment - extends infer X
2020-11-17 [React] Broadcaster + Operator + Listener pattern -- 19. useBroadcaster & useListener
2020-11-17 [Javascript] Broadcaster + Operator + Listener pattern -- 18. Create a Win Condition with a mapDone Operator
2017-11-17 [ES6] Symbol
2016-11-17 [Angular 2] Set Values on Generated Angular 2 Templates with Template Context
2016-11-17 [Angular2 Router] Resolving route data in Angular 2
点击右上角即可分享
微信分享提示