(function () {
'use strict';
})(globalVars);
(Immediately Invoked Function Expression)
var module = module || {};
(function (module) {
'use strict';
module.something = function() {
// Do something interesting
};
})(module);
// avoid
_someFunction();
var someVariable = 3;
// I'm hoisted
function _someFunction() {
// do something cool
}
// recommended
var someVariable;
function _someFunction() {
// do something cool
}
someVariable = 3;
_someFunction();
// avoid
angular
.module('app', [])
.controller('MainCtrl', function MainCtrl() {
})
.service('SomeService', function SomeService() {
});
// recommended
function MainCtrl() {
}
function SomeService() {
}
angular
.module('app', [])
.controller('MainCtrl', MainCtrl)
.service('SomeService', SomeService);
// avoid
$stateProvider.state('main', {
url: "/main",
templateUrl: '/app/main.html',
controller: 'MainCtrl'
});
function MainCtrl($scope) {
$scope.someObject = {};
$scope.doSomething = function () {
};
}
// recommended
$stateProvider.state('main', {
url: "/main",
templateUrl: '/app/main.html',
controller: 'MainCtrl', // or MainCtrl as vm
controllerAs: 'vm'
});
function MainCtrl() {
var vm = this;
vm.someObject = {};
vm.doSomething = function () {
};
}
// avoid
function MainCtrl() {
var vm = this;
vm.doSomething = function() {
if (vm.benchPress < 300) {
return 'Ha ha ha';
}
return 'not bad!';
}
}
// recommended
function MainCtrl(someService) {
var vm = this;
vm.doSomething = function() {
someService.doSomething();
}
}
// avoid
function AnotherService() {
var someValue = '';
return {
someValue: someValue,
};
}
angular
.module('app')
.factory('AnotherService', AnotherService);
// recommended
function AnotherService() {
var AnotherService = {};
AnotherService.someValue = '';
return AnotherService;
}
angular
.module('app')
.factory('AnotherService', AnotherService);
// Presentation
angular.module(appName).directive('brkShowSomething', [_brkShowSomething]);
function _brkShowSomething() {
var directive = {
restrict: 'E',
};
return directive;
}
// <brk-show-something></brk-show-something>
// Add behavior
angular.module(appName).directive('brkDoSomething', [_brkDoSomething]);
function _brkDoSomething() {
var directive = {
restrict: 'A',
};
return directive;
}
// <brk-show-something brk-do-something></brk-show-something>
angular.module(appName).directive('brkMyBadAssDirective', [_brkMyBadAssDirective]);
function brkMyBadAssDirective() {
var directive = {
restrict: 'E',
link: _link,
controller: Controller,
controllerAs: 'vm'
};
return directive;
}
function _link(scope, element, attrs) {
// ALL dom manipulation goes here
// wiring up events, etc...
// I don't get injected but I have access to element
}
Controller.$inject = ['SomeService'];
function Controller(someService) {
// I'm doing logic and data binding type stuff
// I execute before link does
}