Reputation: 1850
Simple question about syntax. In Angular, I've seen functions inside a controller created like this:
this.multiply = function multiply(a, b) {
return a * b;
}
I'm a bit thrown of since the function is given a name and also assigned to a variable with. So my question is why are functions assigned to variables in Angular? Does this affect scope?
Upvotes: 0
Views: 1010
Reputation: 42669
This is standard JavaScript and this is called Named function Expression.
By using this
you are defining it on controller. If you use controlleras
syntax in views or while defining routes you can use such functions as well as any properties defined on this
.
Earlier versions of Angular only employed $scope
and everything had to be defined on $scope
.
Now the controller itself is instantiated on scope depending upon controller name alias in controlleras
Upvotes: 3
Reputation: 190956
It isn't assigning it to a variable. It is assigning it to the instance of the controller, with this
; so whoever has an instance of the controller can call it.
Upvotes: 1