Last active
December 8, 2021 11:20
-
-
Save juniorgarcia/9c708434e7faef47fb491021885b19c5 to your computer and use it in GitHub Desktop.
JS 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
const person = { | |
name: 'John Doe', | |
age: 42, | |
} | |
const logService = (message) => { | |
console.log(`INFO: ${message}`) | |
} | |
/** | |
* Vamos fazer aqui um proxy que loga todos os acessos às propriedades de | |
* `person` | |
*/ | |
const logHandler = { | |
/** | |
* `get` é disparado sempre que você acessa qualquer propriedade de `target`. | |
* @param {*} target o objeto associado ao proxy | |
* @param {*} prop a propriedade sendo acessada | |
*/ | |
get(target, prop) { | |
logService(`acessando a propriedade "${prop}"`) | |
return target[prop] | |
}, | |
} | |
/** | |
* Aqui criamos um Proxy que utilizará as definições de `logHandler` para | |
* alterar as operações básicas de `person`. | |
*/ | |
const personProxy = new Proxy(person, logHandler) | |
// Testando o proxy | |
console.log(person.name) // "John Doe" | |
// Loga: 'INFO: acessando a propriedade "name"' e então 'John Doe' | |
console.log(personProxy.name) |
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
const user = { | |
name: 'John Doe', | |
password: 'secret', | |
} | |
/** | |
* Esse proxy evita que o campo de senha seja logado | |
*/ | |
const obfustacePasswordProxy = { | |
get(target, prop) { | |
if (prop === 'password') return '*'.repeat(target[prop].length) | |
return target[prop] | |
}, | |
} | |
const userProxy = new Proxy(user, obfustacePasswordProxy) | |
// Testando o proxy | |
console.log(user.password) // "secret" | |
console.log(userProxy.password) // "******" |
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
const user = {} | |
/** | |
* Esse proxy transforma todas as propriedades associadas em uppercase, quando | |
* seu tipo é string | |
*/ | |
const uppercaseProxy = { | |
set(target, prop, value) { | |
if (typeof value === 'string') { | |
target[prop] = value.toUpperCase() | |
} else { | |
// Reflect.set nesse caso seria equivalente a "target[prop] = value" | |
// Da mesma forma que existe Reflect.set, existe Reflect.get, que faz | |
// basicamente a mesma coisa só que para obtenção de valores. | |
Reflect.set(...arguments) | |
} | |
}, | |
} | |
const uppercasePerson = new Proxy(user, uppercaseProxy) | |
user.age = 42 | |
user.name = 'John Doe' | |
console.log(user) // { age: 42, name: 'John Doe' } | |
uppercasePerson.age = 32 | |
uppercasePerson.name = 'John Appleseed' | |
console.log(uppercasePerson) // { age: 32, name: 'JOHN APPLESEED' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment