Skip to content

Commit 1afeb12

Browse files
committed
Improve code examples
1 parent edb5bb9 commit 1afeb12

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

JavaScript/4-has.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ const person = new Proxy(data, {
1010
get(obj, key) {
1111
console.log('get', key);
1212
if (key === 'age') {
13-
return (
14-
new Date().getFullYear() - new Date(obj.born.toString()).getFullYear()
15-
);
13+
const current = new Date().getFullYear();
14+
const year = new Date(obj.born.toString()).getFullYear();
15+
return current - year;
1616
}
1717
return obj[key];
1818
},
1919
});
2020

21-
console.log("Try 'age' in person");
21+
console.log('Try "age" in person');
2222
if ('age' in person) {
2323
console.log('Try person.age');
2424
if (person.age) {
25-
console.log("Try person['age']");
25+
console.log('Try person["age"]');
2626
if (person['age']) {
2727
console.log({
2828
born: person.born,

JavaScript/8-pattern.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
'use strict';
22

3-
class Class1 {
4-
method(par1, par2) {
5-
return { par1, par2 };
3+
class Person {
4+
constructor(name, surname) {
5+
this.name = name;
6+
this.surname = surname;
7+
}
8+
9+
getFullName() {
10+
const { name = '', surname = '' } = this;
11+
return name + ' ' + surname;
612
}
713
}
814

9-
class ProxyClass {
10-
constructor(instance) {
11-
this.instance = instance;
15+
class PersonProxy {
16+
constructor(person) {
17+
this.person = person;
1218
}
1319

14-
method(par1, par2) {
15-
return this.instance.method(par1, par2);
20+
getFullName() {
21+
let fullName = this.person.getFullName();
22+
if (fullName.startsWith(' ') || fullName.endsWith(' ')) {
23+
fullName = fullName.trim();
24+
}
25+
return fullName;
1626
}
1727
}
1828

1929
// Usage
2030

21-
const proxy = new ProxyClass(new Class1());
22-
const res = proxy.method('value1', 'value2');
23-
console.dir(res);
31+
const proxy = new PersonProxy(new Person('Marcus', 'Antonin'));
32+
const fullName = proxy.getFullName();
33+
console.dir({ fullName });

0 commit comments

Comments
 (0)