Nicolas M. Pardo
Full-Stack Developer, Front-End Lover and UI Designer
(data.a === 1 && data.a === 2 && data.a === 3)
// true
github.com/Nikodermus/bogotajs-proxies
It's the answer that ES6 created to the desire to intercept objects as the NSA intercepts phones
Es la solución de ES6 para interceptar los objetos como el gobierno intercepta telefonos
const person = {
name: 'Nico',
age: '21',
};
const handler = {
get(target, key, name) {
console.log(target, key, name)
return `How about no`;
},
};
const proxy_person = new Proxy(person, handler);
Actual object
Proxy
External Methods
const phone_handler = {
set(target, key, value) {
console.log(value);
target[key] = value.match(/\d/g).join('');
},
get(target, key, value) {
if (!target[key]) return undefined;
return target[key]
.replace(/(\d{3})(\d{3})(\d{4})/, '($1)-$2-$3');
},
};
const phone_numbers = new Proxy({}, phone_handler);
set(target, key, value) {
const maybe = Object.keys(target)
.find(obj_key => obj_key.toLowerCase() === key.toLowerCase());
if (!target[key] && maybe) {
throw new Error(`Try with '${maybe}' instead`);
}
return target[key];
}
Are they trying to set something new that already exists?
¿Están tratando de crear algo nuevo que ya existe?
get(target, key, value) {
const maybe = Object.keys(target)
.find(obj_key => obj_key.toLowerCase() === key.toLowerCase());
if (typeof target[key] === 'undefined' && maybe) {
console.warn(`'Returning ${maybe} instead'`);
return target[maybe];
}
return target[key];
},
They're trying to get something that doesn't exist but there's something similar
Tratan de obtener algo que no existe, pero hay algo similar
api.get();
//Not defined
api.getUsers();
api.getUsers$Likes('1234');
api.getUsers$Likes('1234', {
page: 2,
});
api.postItems({
name: 'Item name',
});
None of the following methods are defined within the API but all will resolve th desired value.
Ninguno de los siguientes metodos están definidos pero todos se resuelven al valor deseado
Original from: https://goo.gl/p3L2ED
const handler = {
get: (target, name) => target[name] += 1,
};
const data = new Proxy({}, handler);
data.a = 1; //1
console.log(data.a); //2
console.log(data.a); //3
.block {}
.block__element {}
.block--modifier {}
.block__element--modifier {}
Taken from MindBEMding: https://goo.gl/dWQBxs
<div class="media">
<img src="logo.png" alt="Foo Corp logo" class="img-rev">
<div class="body">
<h3 class="alpha">Welcome to Foo Corp</h3>
<p class="lede">Foo Corp is the best, seriously!</p>
</div>
</div>
<div class="media">
<img src="logo.png" alt="Foo Corp logo"
class="media__img media__img--rev">
<div class="media__body">
<h3 class="alpha">Welcome to Foo Corp</h3>
<p class="lede">Foo Corp is the best, seriously!</p>
</div>
</div>
.btn
border: 1px solid red
padding: 1rem
text-align: center
width: 100px
//Modifier
&--fluid
width: 100%
//Modifier
&--blue
background: blue
//Block
&__icon
font-size: 2rem
//Block modifier
&--large
font-size: 3rem
btn--fluid btn__icon btn__icon--large
btn btn--blue
btn btn--blue
/Nikodermus
nicolasm@dakio.co
By Nicolas M. Pardo
ES6 bring us Proxy Objects, what are those? How can we use them?