[TYPESCRIPT] A practical guide to TypeScript decorators exmaple (ok)

https://app.gitbook.com/@javascriptuse/s/advanced/typescript-mot-so-scripts-hay-duoc-su-dung-trong-typescripts-ok

Ví dụ 1: sử dụng decorators

function delay() {
	console.log("delay(): evaluated");
	return function(target,propertyKey: string, description: PropertyDescription) {
		console.log("delay(): called");
	}
}
function other() {
	console.log("other(): evaluated");
	return function(target,propertyKey: string, description: PropertyDescription) {
		console.log("other(): called");
	}
}
class TimeOut {
	count: number = 1;
	@delay();
	@other();
	callMe() {
		this.count ++;
		console.log('callMe is been called with', this.count);
	}
}
let timeout = new TimeOut();
console.log('going to call this method callMe');
timeout.callMe();
console.log('after callMe is been called', timeout.count);

Ví dụ 2: sử dụng decorators

C:\Users\Administrator\Desktop\typescript-ru6t7p\index.ts

C:\Users\Administrator\Desktop\typescript-ru6t7p\id.decorator.ts

C:\Users\Administrator\Desktop\typescript-ru6t7p\package.json

Ví dụ 3: sử dụng decorators

C:\Users\Administrator\Desktop\typescript-decorators\calculate-time.ts

C:\Users\Administrator\Desktop\typescript-decorators\package.json

C:\Users\Administrator\Desktop\typescript-decorators\tsconfig.json

Ví dụ 4: sử dụng decorators

C:\Users\Administrator\Desktop\typescript-decorators\error-guard.ts

Last updated

Was this helpful?