File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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' ) ;
2222if ( '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 ,
Original file line number Diff line number Diff line change 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 } ) ;
You can’t perform that action at this time.
0 commit comments