I'm beginning using AngularJS (v1) framework, and after browsing good practices I found those examples (from this page https://github.com/johnpapa/angular-styleguide/tree/master/a1), like :
/* recommended */
function SessionsController() {
var vm = this;
vm.gotoSession = gotoSession;
vm.refresh = refresh;
vm.search = search;
vm.sessions = [];
vm.title = 'Sessions';
////////////
function gotoSession() {
/* */
}
function refresh() {
/* */
}
function search() {
/* */
}
}
Am I the only one to find this weird ? It looks like bad practices in JavaScript.
Each time you create a controller, you'll have to write functions and also write all accessors to all of them. It's looks useless and also less readable as it mixes methods attribution and attributes.
Each time this controller is instantiated, all methods will be rewritten. Shouldn't we use a prototype to share methods between all instances ?
function SessionsController() {
this.sessions = [] ;
this.title = 'Sessions' ;
}
SessionsController.prototype = {
gotoSession : function() {
/* */
},
refresh : function() {
/* */
},
search : function() {
/* */
}
} ;
Isn't it simpler, more readable and more efficient ?
Maybe I've missed something. What do you think ?