In-depth Understanding of ES6 012 [Study Notes]

Proxy and Reflection API
A Proxy is a wrapper that can intercept and modify underlying JavaScript engine operations. It exposes internal operational objects, enabling developers to create custom built-in objects.

Proxy Traps
Overridden Features | Default Behavior
get Reads a property value | Reflect.get()
set Writes a property value | Reflect.set()
has `in` operator | Reflect...

Proxy and Reflection API

A proxy is a wrapper that can intercept and modify underlying JavaScript engine operations. In new languages, it exposes internal operational objects, allowing developers to create built-in objects.

Proxy Trap Overridden Feature Default Feature
get Read a property value Reflect.get()
set Write a property value Reflect.set()
has in operator Reflect.has()
deleteProperty delete operator Reflect.deleteProperty()
getPropertyOf Object.getPropertyOf() Reflect.getPropertyOf()
setPropertyOf Object.setPropertyOf() Reflect.setPropertyOf()
isExtensible Object.isExtensible() Reflect.isExtensible()
preventExtensions Object.preventExtensions() Reflect.preventExtensions()
getOwnPropertyDescriptor Object.getOwnPropertyDescriptor() Reflect.getOwnPropertyDescriptor()
defineProperty Object.defineProperty() Reflect.defineProperty()
ownKeys Object.keys()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Reflect.ownKeys()
apply Call a function Reflect.apply()
construct Call a function with new Reflect.construct()

## Creating a simple proxy

 let target = {}
 let proxy = new Proxy(target,{}),

 proxy.name = "proxy";
 console.log(proxy.name) // proxy
 console.log(target.name) // proxy

 target.name = "target"
console.log(proxy.name) // target
 console.log(target.name) // target

Using the set trap to validate properties

Accepts 4 arguments

  • trapTarget: The object that receives the property (the proxy's target)
  • key: The property key to be written
  • value: The value of the property being written
  • receiver: The object on which the operation occurred (usually the proxy)
let target = {
  name:"target"
}
let proxy = new Proxy(target,{
  set(trapTarget,key,value,recevier){
    // Ignore existing properties that are not intended to be affected
    if(!trapTarget.hasOwnProperty(key)){
      if(isNaN(value)){
        throw new TypeError('Property must be a number')
      }
    }
    // Add property
    return Reflect.set(trapTarget,key,value,recevier)
  }
})
proxy.count = 1;
console.log(proxy.count) // 1
console.log(target.count) // 1

// Since the target already has a name property, it can be assigned a value
proxy.name = "proxy"
console.log(proxy.name) // proxy
console.log(target.name) // proxy
// Assigning a value to a non-existent property will throw an error
proxy.otherName = "proxy"

I don't want to look at the rest... maybe later.

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/4340

(0)
Walker的头像Walker
上一篇 Mar 8, 2025 12:52
下一篇 Mar 8, 2025 12:52

Related Posts

  • Go Engineer System Course 018 [Study Notes]

    Getting Started with API Gateway and Continuous Deployment (Kong & Jenkins) corresponds to the course materials "Chapter 2: Getting Started with Jenkins" and "Chapter 3: Deploying Services with Jenkins", outlining the practical path for Kong and Jenkins in enterprise-level continuous delivery. Even with zero prior experience, you can follow the steps to build your own gateway + continuous deployment pipeline. Pre-class Introduction: What is an API Gateway? An API Gateway sits between clients and backend microservices...

    Personal Nov 25, 2025
    19000
  • In-depth Understanding of ES6 006 [Study Notes]

    Symbol and Symbol properties The 6th primitive data type: Symbol. Private names were originally designed to allow developers to create non-string property names, but general techniques cannot detect the private names of these properties. Creating a Symbol let firstName = Symbol(); let person = {} person[firstName] = "Nicholas"; cons…

    Personal Mar 8, 2025
    1.2K00
  • Nuxt3: Beginner's Guide and Principles Introduction [Learning Notes]

    Nuxt 3: Getting Started and Principles 💡 What is Nuxt 3? Nuxt 3 is a full-stack frontend framework built on Vue 3 and Vite, supporting: Server-Side Rendering (SSR) Static Site Generation (SSG) Single-Page Applications (SPA) Building full-stack applications (with API support) Nuxt 3 is an "enhanced version" of Vue, helping you simplify project structure and development workflow. 🔧 Core Principles Feature How Nuxt Handles It ✅ Page Routing Automatic root...

    Personal Apr 6, 2025
    2.1K00
  • Go Engineering Systematic Course 003 [Study Notes]

    grpc grpc grpc-go grpc seamlessly integrates protobuf protobuf. For those of you accustomed to using JSON and XML data storage formats, I believe most have never heard of Protocol Buffer. Protocol Buffer is actually a lightweight & efficient structured data storage format developed by Google, and its performance is truly much, much stronger than JSON and XML! protobuf…

    Personal Nov 25, 2025
    18800
  • In-depth Understanding of ES6 009 [Learning Notes]

    Classes in JavaScript function PersonType(name){ this.name = name; } PersonType.prototype.sayName = function(){ console.log(this.name) } var person = new PersonType("Nicholas") p…

    Personal Mar 8, 2025
    1.2K00
EN
简体中文 繁體中文 English
欢迎🌹 Coding never stops, keep learning! 💡💻 光临🌹