1
是否有通過裝飾添加功能的有效方法?通過裝飾添加功能
裝飾:
function testDecorator(options){
return function(target){
target.test = function() {
console.log('Zipp Zapp!');
};
}
}
類別:
@testDecorator({})
class Book{
}
使用(在這種情況下優選的),如在
Book.test()
打字稿編譯結果:
Property 'test' does not exist on type 'typeof Book'.
使用像
var b = new Book();
b.test();
打字稿編譯結果:
Property 'test' does not exist on type 'Book'