【发布时间】:2012-11-29 05:16:00
【问题描述】:
假设我有这样的事情:
interface Scope extends ng.Scope {
getMeSome(id:string):number[];
}
export class AwesomeController {
constructor (public $scope:Scope) {
$scope.getMeSome = id => this.getMeSome(id);
}
getMeSome(id:string){
console.log('Alright... alright... here it is');
}
}
现在你可以看到我在 3 个不同的地方有相同的方法签名。当然,我可以把它剪掉一点,然后在构造函数中执行它:
constructor (public $scope:Scope) {
$scope.getMeSome = id => {
console.log('Alright... alright... here it is');
};
}
但这会让构造函数的身体像打了类固醇一样(如果你有几十种不同的方法)。 所以我想知道为什么我不能这样做:
export class AwesomeController {
constructor (public $scope:Scope) { }
$scope.getMeSome(id:string) { // Can't extend $scope here, although I can do that in the constructor
console.log('Alright... alright... here it is');
}
}
为什么这不起作用?有什么让它更性感的建议吗?
【问题讨论】:
标签: typescript