1.监听对象属性的操作(Es5)
const obj = { name: "why", age: 18, height: 1.88 } // 需求: 监听对象属性的所有操作 // 监听属性的操作 // 1.针对一个属性 // let _name = obj.name // Object.defineProperty(obj, "name", { // set: function(newValue) { // console.log("监听: 给name设置了新的值:", newValue) // _name = newValue // }, // get: function() { // console.log("监听: 获取name的值") // return _name // } // }) // 2.监听所有的属性: 遍历所有的属性, 对每一个属性使用defineProperty const keys = Object.keys(obj) for (const key of keys) { let value = obj[key] Object.defineProperty(obj, key, { set: function(newValue) { console.log(`监听: 给${key}设置了新的值:`, newValue) value = newValue }, get: function() { console.log(`监听: 获取${key}的值`) return value } }) } // console.log(obj.name) // obj.name = "kobe" console.log(obj.age) obj.age = 17 console.log(obj.age) // 什么是响应式? // const nameEl = document.querySelector(".name") // nameEl.textContent = obj.name // obj.name = "kobe" // obj.name = "james"
2.监听对象属性的操作(Es6)
const obj = { name: "why", age: 18, height: 1.88 } // 1.创建一个Proxy对象 const objProxy = new Proxy(obj, { set: function(target, key, newValue) { console.log(`监听: 监听${key}的设置值: `, newValue) target[key] = newValue }, get: function(target, key) { console.log(`监听: 监听${key}的获取`) return target[key] } }) // 2.对obj的所有操作, 应该去操作objProxy // console.log(objProxy.name) // objProxy.name = "kobe" // console.log(objProxy.name) // objProxy.name = "james" objProxy.address = "广州市" console.log(objProxy.address)
3.Proxy-其他捕获器的监听方法
const obj = { name: "why", age: 18, height: 1.88 } // 1.创建一个Proxy对象 const objProxy = new Proxy(obj, { set: function(target, key, newValue) { console.log(`监听: 监听${key}的设置值: `, newValue) target[key] = newValue }, get: function(target, key) { console.log(`监听: 监听${key}的获取`) return target[key] }, deleteProperty: function(target, key) { console.log(`监听: 监听删除${key}属性`) delete obj.name }, has: function(target, key) { console.log(`监听: 监听in判断 ${key}属性`) return key in target } }) delete objProxy.name console.log("age" in objProxy)
4.Proxy-监听函数对象的操作
function foo(num1, num2) { console.log(this, num1, num2) } const fooProxy = new Proxy(foo, { apply: function(target, thisArg, otherArgs) { console.log("监听执行了apply操作") target.apply(thisArg, otherArgs) }, construct: function(target, otherArray) { console.log("监听执行了new操作") console.log(target, otherArray) return new target(...otherArray) } }) // fooProxy.apply("abc", [111, 222]) new fooProxy("aaa", "bbb")
5.Reflect-和Object的区别之一
"use strict" const obj = { name: "why", age: 18 } Object.defineProperty(obj, "name", { configurable: false }) // Reflect.defineProperty() // 1.用以前的方式进行操作 // delete obj.name // if (obj.name) { // console.log("name没有删除成功") // } else { // console.log("name删除成功") // } // 2.Reflect if (Reflect.deleteProperty(obj, "name")) { console.log("name删除成功") } else { console.log("name没有删除成功") }
6.Reflect-和Proxy共同完成代理
const obj = { name: "why", age: 18 } const objProxy = new Proxy(obj, { set: function(target, key, newValue, receiver) { // target[key] = newValue // 1.好处一: 代理对象的目的: 不再直接操作原对象 // 2.好处二: Reflect.set方法有返回Boolean值, 可以判断本次操作是否成功 const isSuccess = Reflect.set(target, key, newValue) if (!isSuccess) { throw new Error(`set ${key} failure`) } }, get: function(target, key, receiver) { } }) // 操作代理对象 objProxy.name = "kobe" console.log(obj)
7.Reflect-Reflect设置receiver
const obj = { _name: "why", set name(newValue) { console.log("this:", this) // 默认是obj this._name = newValue }, get name() { return this._name } } // obj.name = "aaaa" // console.log(obj.name) // obj.name = "kobe" const objProxy = new Proxy(obj, { set: function(target, key, newValue, receiver) { // target[key] = newValue // 1.好处一: 代理对象的目的: 不再直接操作原对象 // 2.好处二: Reflect.set方法有返回Boolean值, 可以判断本次操作是否成功 /* 3.好处三: > receiver就是外层Proxy对象 > Reflect.set/get最后一个参数, 可以决定对象访问器setter/getter的this指向 */ console.log("proxy中设置方法被调用") const isSuccess = Reflect.set(target, key, newValue, receiver) if (!isSuccess) { throw new Error(`set ${key} failure`) } }, get: function(target, key, receiver) { console.log("proxy中获取方法被调用") return Reflect.get(target, key, receiver) } }) // 操作代理对象 objProxy.name = "kobe" console.log(objProxy.name)
8.Reflect-和construct结合的反射
function Person(name, age) { this.name = name this.age = age } function Student(name, age) { // Person.call(this, name, age) const _this = Reflect.construct(Person, [name, age], Student) return _this } // const stu = new Student("why", 18) const stu = new Student("why", 18) console.log(stu) console.log(stu.__proto__ === Student.prototype)
标签:obj,name,Reflect,详解,Proxy,key,target,console,log From: https://www.cnblogs.com/yoyo1216/p/17048614.html