I am trying to learn descriptors in Angular. I am trying this piece of code just to see how descriptors can manipulate the original methods. But I can see that the @squareLog funtion's "console.log()" statement is getting executed first and rest of the code executes in order. I am 100% noob in Typescript and Angular. Is the output correct? Is this what should have been expected from this code? If no,then did I messed up somewhere down the line?
import { Component } from '@angular/core';
function log(target, name, descriptor){
//store the original function in a variable
const original = descriptor.value;
//do some manipulation with descriptor.value
descriptor.value = function(){
console.log("This function is with LOG as the descriptor");
}
//return the descriptor
return descriptor;
}
function squareLog (target, name, descriptor){
console.log("This function is with SQUARELOG as descriptor");
const original = descriptor.value;
descriptor.value = function(...args){
console.log("Arguments ", args, "were passed in this function");
const result = original.apply(this, args);
console.log("The result of the function is ", result);
return result;
}
return descriptor;
}
function classLog(className){
console.log(className);
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
@classLog
export class AppComponent {
title: string = 'Demonstration of decriptors';
constructor(){
this.aSimpleMethod();
console.log("This statement was generated by constructor: ", this.returnSquare(5));
}
@log
aSimpleMethod(){
console.log("Learning about descriptors");
}
@squareLog
returnSquare(a){
return a*a;
}
}
