Let’s say I have something like this:
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');
}
}
Now as you can see I have the same method signature in 3 different places. Of course I can cut it down a bit and do that right there – in the constructor:
constructor (public $scope:Scope) {
$scope.getMeSome = id => {
console.log('Alright... alright... here it is');
};
}
But that will blow the constructor’s body like it’s on steroids (if you have tens of different methods).
So I was wondering why can’t I do something like that:
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');
}
}
Why this doesn’t work? Any suggestions to make it sexier?
So far as I can tell, in classes (unlike in modules), you’re defining the class, not running the class, so you can’t do any assignments in the body of the class (as opposed to method bodies). So this syntax wouldn’t work:
You’re also just defining the methods of this class, not of other classes, so the syntax you have above wouldn’t work either, since it’s mucking about with the method of a different class at the spot where you’re expected to just be defining your own:
I think you’ll just have to do it one of the first two ways that you proposed – which both look just fine to me.