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 10, 2026 00:00
下一篇 Mar 8, 2026 15:40

Related Posts

  • Go Engineer System Course 007 [Study Notes]

    Goods Microservice Entity Structure Description This module contains the following core entities: Goods (Goods) Goods Category (Category) Brand (Brands) Carousel (Banner) Goods Category Brand (GoodsCategoryBrand) 1. Goods (Goods) Describes the product information actually displayed and sold on the platform. Field Description Field Name Type Description name String Product name, required brand Pointer …

    Personal Nov 25, 2025
    28300
  • Go Engineer System Course 006 [Study Notes]

    Project Structure Description: The user-web module is the user service Web layer module within the joyshop_api project, responsible for handling user-related HTTP requests, parameter validation, business routing, and calling backend interfaces. Below is the directory structure description: user-web/ ├── api/ # Controller layer, defines business interface processing logic ├── config/ # Configuration module, contains system configuration structs and reading logic ...

    Personal Nov 25, 2025
    28600
  • Deep Dive into ES6 010 [Study Notes]

    Improved array functionality. The peculiar behavior of `new Array()`: when a single numeric value is passed to the constructor, the array's `length` property is set to that value; if multiple values are passed, regardless of whether they are numeric or not, they all become elements of the array. This behavior is confusing, as it's not always possible to pay attention to the type of data passed in, thus posing a certain risk. `Array.of()`, regardless of how many arguments are passed, has no special case for a single numeric value (one argument and numeric type); it always returns an array containing all arguments...

    Personal Mar 8, 2025
    1.3K00
  • Node: Demystified (Shengsi Garden Education) 001 [Learning Notes]

    Node.js: Understanding it from an Asynchronous Programming Paradigm
    Node.js's Positioning and Core Philosophy
    Based on the V8 engine + libuv event-driven library, it brings JavaScript from the browser to the server side. It uses a single-threaded event loop to handle I/O, maximizing CPU time slices while waiting for I/O, making it particularly suitable for high-concurrency, I/O-intensive scenarios. "Don't block the main thread" is its design philosophy: try to offload time-consuming operations to the kernel or a thread pool, and callback results...

    Personal Nov 24, 2025
    34300
  • Go Engineering Systematic Course 014 [Study Notes]

    RocketMQ Quick Start. Go to our various configurations (podman) to see how it's installed. Introduction to Concepts: RocketMQ is a distributed messaging middleware open-sourced by Alibaba and an Apache top-level project. Core components: NameServer: Service discovery and routing; Broker: Message storage, delivery, and fetching; Producer: Message producer (sends messages); Consumer: Message consumer (subscribes to and consumes messages); Topic/Tag: Topic/...

    Personal Nov 25, 2025
    24300
EN
简体中文 繁體中文 English