forked from shama/letswritecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (40 loc) · 1.19 KB
/
index.js
File metadata and controls
57 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
let bears = { grizzly: true }
let grizzlyCount = 0
const proxyBears = new Proxy(bears, {
get: function (target, prop, receiver) { // receiver is the proxy object itself, which is proxyBears
if (prop === 'grizzly') grizzlyCount++
return Reflect.get(target, prop, receiver) //instead of returning target[prop] we can use Reflect like this
// return target[prop]
},
set: function (target, prop, value, receiver) {
if (['grizzly', 'brown', 'polar'].indexOf(prop) === -1) {
throw new Error('THAT IS TOTALLY NOT A BEAR!')
}
return Reflect.set(target, prop, value, receiver)
// target[prop] = value
},
deleteProperty: function (target, prop) {
console.log(`You have deleted ${prop}`)
delete target[prop]
}
})
//proxyBears.aardvark = true
proxyBears.polar = true
proxyBears.brown = false
//delete proxyBears.polar
//console.log(proxyBears.polar)
console.log(bears)
proxyBears.grizzly
proxyBears.grizzly
proxyBears.grizzly
proxyBears.grizzly
console.log(grizzlyCount)
function growl() {
return 'grrr'
}
const loudGrowl = new Proxy(growl, {
apply: function (target, thisArg, args) {
return target().toUpperCase() + '!!!'
}
})
console.log(loudGrowl())