const handler = {
defineProperty(target, prop, descriptor) {
console.log('trigger defineProperty')
return Reflect.defineProperty(target, prop, descriptor)
},
deleteProperty(target, prop) {
console.log('trigger deleteProperty')
return Reflect.deleteProperty(target, prop)
},
set(target, prop, value, receiver) {
console.log('trigger setter & prop => ', prop)
return Reflect.get(target, prop, value, receiver)
},
get(target, prop, receiver) {
console.log('trigger getter & prop => ', prop)
return Reflect.get(target, prop, receiver)
},
has(target, prop) {
console.log('trigger has')
return Reflect.has(target, prop)
},
ownKeys(target) {
console.log('trigger ownKeys')
return Reflect.ownKeys(target)
},
getOwnPropertyDescriptor(target, prop) {
console.log('trigger getOwnPropertyDescriptor & prop => ', prop)
return Reflect.getOwnPropertyDescriptor(target, prop)
},
setPrototypeOf(target, prototype) {
console.log('trigger setPrototypeOf')
return Reflect.setPrototypeOf(target, prototype)
},
getPrototypeOf(target) {
console.log('trigger getPrototypeOf')
return Reflect.getPrototypeOf(target)
},
preventExtensions(target) {
console.log('trigger preventExtensions')
return Reflect.preventExtensions(target)
},
isExtensible(target) {
console.log('trigger isExtensible')
return Reflect.isExtensible(target)
},
apply(target, thisArg, argumentsList) {
console.log('trigger apply')
return Reflect.apply(target, thisArg, argumentsList)
},
construct(target, argumentsList, newTarget) {
// console.log(newTarget === proxy)
console.log('trigger construct')
return Reflect.construct(target, argumentsList, newTarget)
}
}
Object 属性与方法
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
Proxy 处理程序
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/construct
属性
Object.prototype.constructor
const foo = function() {
return 1 + 1
}
const proxy = new Proxy(foo, handler)
console.log(proxy.constructor)
// trigger getter & prop => constructor
// [Function: Function]
方法
Object.assign()
const prototype = {
num: 11 }
const foo = Object.create(prototype)
foo.id = 'foo'
const symbol = Symbol(foo)
Object.defineProperties(foo, {
'age': {
value: 19,
enumerable: false,
configurable: true,
writable: true
},
'sex': {
get: function() {
return 'man'
},
set() {
},
enumerable: true,
configurable: true
},
[symbol]: {
value: 'symbol-value',
writable: true,
enumerable: true,
configurable: true
}
})
const proxy = new