Created
November 27, 2020 13:29
-
-
Save minhntm/48ff80c6c03958cedf6c7f833ca73ef7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function uppercase( target: any, name: string, desc: PropertyDescriptor ) { | |
return { | |
get: function (): string { | |
return desc.get?.call( this ).toUpperCase(); | |
}, | |
set: function ( name: string ) { | |
desc.set?.call( this, name.split(' ') ); | |
} | |
}; | |
} | |
function capitalize(target: any, name: string, desc: PropertyDescriptor){ | |
return { | |
value: function (): string { | |
const str = desc.value.call(this); | |
return str[0].toUpperCase() + str.slice(1); | |
} | |
} | |
} | |
function addVersion<T extends { new (...args: any[]): any }>(constructor: T): T{ | |
return class extends constructor { | |
static version: string = '1.0'; | |
} | |
} | |
@addVersion | |
class Person { | |
constructor( | |
public fname: string, public lname: string, | |
) {} | |
@capitalize | |
getFirstName(): string { | |
return this.fname; | |
} | |
@capitalize | |
getLastName(): string { | |
return this.lname; | |
} | |
@uppercase | |
get fullname(): string { | |
return this.fname + ' ' + this.lname; | |
} | |
set fullname( [ fname, lname ] ) { | |
this.fname = fname; | |
this.lname = lname; | |
} | |
} | |
var person = new Person( 'ross', 'geller' ); | |
console.log( 'fullname ->', person.fullname ); | |
console.log( 'first name ->', person.getFirstName()); | |
console.log( 'last name ->', person.getLastName()); | |
console.log( 'version ->', (Person as any).version); | |
// console.log( 'person ->', person ); | |
person.fullname = 'chandler bing'; | |
console.log( 'fullname* ->', person.fullname ); | |
// console.log( 'person* ->', person ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment