Skip to content

API Proxy

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

Proxy

动态对象访问拦截

Dynamic object-access interception

脚本 API 目录

Script API Directory

概述

Overview

Proxy 包装一个目标对象,并将属性读取和写入交给处理器。选项对象必须提供 getset;处理器应保持明确、无递归副作用,以避免难以追踪的行为。

Proxy wraps a target object and delegates property reads and writes to handlers. The options object must provide get and set; handlers should be explicit and free of recursive side effects.

new Proxy(target, options)

Parameters:

  • target
    被包装的对象。

    Object to wrap.

  • options
    包含 get(obj, key)set(obj, key, value) 的对象。

    Object containing get(obj, key) and set(obj, key, value).

Returns

Proxy 对象。

A Proxy object.

var source = { count: 1 };
var proxy = new Proxy(source, {
    get: (obj, key) => obj[key],
    set: (obj, key, value) => { obj[key] = value; return value; }
});
proxy.count = 2;
return proxy.count; // 2

边界行为

缺少 getset 是无效构造;不要在处理器内无条件再次读取或写入同一 Proxy。

Missing get or set is invalid; do not unconditionally re-read or re-write the same proxy from a handler.

Clone this wiki locally