Created
September 19, 2020 23:14
-
-
Save shizuka-na-kazushi/4fc6b304d9a2ce4dec66c7b4bf79435c to your computer and use it in GitHub Desktop.
dog.name プロパティを別の値に置き換えてしまう Proxy !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// トムという名前を持った犬オブジェクトを生成 | |
var dog = {name: 'トム', age: 1}; | |
// 1. 普通に dog を使う | |
console.log(`${dog.name}は、${dog.age} 歳の犬です`); | |
// Proxyに渡すhandler | |
var handler = { | |
get: (target, prop, receiver) => { | |
if (prop == 'name') { | |
return 'ジェリー'; | |
} | |
return target[prop]; | |
} | |
} | |
// proxyで置き換えた! | |
dog = new Proxy(dog, handler); | |
// 2. 置き換わった dog を使う | |
console.log(`${dog.name}は、${dog.age} 歳の犬です`); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment