Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy #47

Open
jinzhepro opened this issue Apr 25, 2023 · 0 comments
Open

Proxy #47

jinzhepro opened this issue Apr 25, 2023 · 0 comments

Comments

@jinzhepro
Copy link
Owner

Proxy可以创建一个对象的代理,实现对操作的劫持。

MDN-Proxy

// 定义一个普通对象
var obj = { 
  a: 100000,
}
// 定义一个处理器对象
var handler = { 
	// 拦截属性读取 如属性值为不可写或不可配置时会抛出Error
  get(target, prop, receiver) { // 目标对象 属性名 Proxy对象
    console.log(target, prop, receiver);
    return target[prop] // 返回一个值
  },
	// 拦截属性设置 如属性值为不可写或不可配置时会抛出Error
  set(target, prop, value, receiver) { // 目标对象 属性名 值 Proxy对象
    console.log(target, prop, value, receiver);
    target[prop] = value
    return true // 返回true未成功,严格模式下返回false会抛出Error
  }
	
	// 拦截delete操作
	deleteProperty(){
}
var proxy = new Proxy(obj, handler)
console.log(proxy.a);
console.log(proxy.a = 1,obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant