[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" */

 

posted @ 2018-09-06 03:22  Zhentiw  阅读(122)  评论(0)    收藏  举报