File tree 2 files changed +26
-16
lines changed
2 files changed +26
-16
lines changed Original file line number Diff line number Diff line change @@ -10,19 +10,19 @@ const person = new Proxy(data, {
10
10
get ( obj , key ) {
11
11
console . log ( 'get' , key ) ;
12
12
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 ;
16
16
}
17
17
return obj [ key ] ;
18
18
} ,
19
19
} ) ;
20
20
21
- console . log ( " Try ' age' in person" ) ;
21
+ console . log ( ' Try " age" in person' ) ;
22
22
if ( 'age' in person ) {
23
23
console . log ( 'Try person.age' ) ;
24
24
if ( person . age ) {
25
- console . log ( " Try person[' age']" ) ;
25
+ console . log ( ' Try person[" age"]' ) ;
26
26
if ( person [ 'age' ] ) {
27
27
console . log ( {
28
28
born : person . born ,
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
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 ;
6
12
}
7
13
}
8
14
9
- class ProxyClass {
10
- constructor ( instance ) {
11
- this . instance = instance ;
15
+ class PersonProxy {
16
+ constructor ( person ) {
17
+ this . person = person ;
12
18
}
13
19
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 ;
16
26
}
17
27
}
18
28
19
29
// Usage
20
30
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 } ) ;
You can’t perform that action at this time.
0 commit comments