Logging records useful runtime information to help you understand program behavior or track down bugs. Scattered logging statements can clutter domain code and be easy to forget when new properties are added. section 8.2.1
A JavaScript Proxy can log property reads and writes through its get and set traps. This keeps logging separate from the object’s domain code and applies the same behavior across its properties. section 8.2 section 8.2.1
Listing 8.9. Using proxies makes it easier to add logging to objects
function makeLoggable(target){ #A
return new Proxy(target, { #B
get: (target, property) => { #C
report("Reading " + property); #C
return target[property]; #C
}, #C
set: (target, property, value) => { #D
report("Writing value " + value + " to " + property); #D
target[property] = value; #D
} #D
});
}
let ninja = { name: "Yoshi"}; #E
ninja = makeLoggable(ninja); #E
assert(ninja.name === "Yoshi", "Our ninja Yoshi"); #F
ninja.weapon = "sword"; #F
See a makeLoggable function wrap a Ninja object in a Proxy that reports property reads and writes. section 8.2.1













