SOURCE

// let target = {
//     x:10,
//     y:20
// }

// let hanler = {
//     get:(obj,prop) => {
//         console.log('obj',obj)
//         console.log('prop',prop)
//         return prop = obj(1,2)
//     }
// }

// let targetFunction = function(a,b){
//     return a+b
// }
// console.log('targetFunction',targetFunction(3,4))
// target =  new Proxy(targetFunction,hanler)

// console.log(target.x)
// console.log(target.y)


// const withZerValue = (target,zeroValue) => new Proxy(target,{
//     get:(obj,prop) =>{
//         return (prop in obj)?obj[prop]:zeroValue
//     }
// })

// let pos = {
//     x:4,
//     y:19
// }

// pos = withZerValue(pos,0)
// console.log(pos.z)

// const negativeArray = (els)=>new Proxy(els,{
//     get:(target,propkey,receiver)=>{
//       return  Reflect.get(target,
//       (+propkey < 0)?String(target.length+ +propkey):propkey,
//       receiver)
//     }
// })

// const lastArr = negativeArray([1,2,3,4])
// console.log(lastArr[2])


const ephemeral = (target,ttl = 60)=>{
    const CREATED_AT = Date.now()
    const isExpired = () => (Date.now()-CREATED_AT)>(ttl*1000)
    return new Proxy(target,{
        get:(obj,prop) => isExpired()?undefined:Reflect.get(obj,prop)
    })
}

let bankAccount = ephemeral({
    balance:14.93
},10)

console.log(bankAccount.balance)
setTimeout(()=>{
console.log(bankAccount.balance)
},10*1000)
console 命令行工具 X clear

                    
>
console