[TypeScript] Understanding Decorators

Decorators are a feature of TypeScript that are becoming more and more common in many major libraries. This lesson walks you through what decorators are and how to create your own.

function addAge(age: number) { 
    return function (targetClass: any) { 
        return class { 
            name = new targetClass().name;
            age = age
        }
    }
}

@addAge(30)
class Person { 
    name = 'Johhn'
}

console.log(new Person()) /**class_1
                                age: 30
                                name: "Johhn" */
原文地址:https://www.cnblogs.com/Answer1215/p/9595917.html