Consider the following example:
angular.module('demo')
.service('MyService', function () {
this.fn = function () {
console.log('MyService:fn');
};
})
.factory('MyFactory', function () {
function fn() {
console.log('MyFactory:fn');
}
return { fn: fn };
})
.value('MyValue', {
fn: function () {
console.log('MyValue:fn');
}
})
.constant('MyConstant', {
fn: function () {
console.log('MyConstant:fn');
}
})
.run(function (MyService, MyFactory, MyValue, MyConstant) {
MyService.fn();
MyFactory.fn();
MyValue.fn();
MyConstant.fn();
MyService.fn = undefined;
MyFactory.fn = undefined;
MyValue.fn = undefined;
MyConstant.fn = undefined;
})
.run(function (MyService, MyFactory, MyValue, MyConstant) {
MyService.fn();
MyFactory.fn();
MyValue.fn();
MyConstant.fn();
});
When the first run()
is executed, all 4 console logs will be executed and print something on the console. Then I set each of the providers fn
function to undefined for simplification purposes, say someone rewrote this function somewhere (which is something I want to prevent).
On the second run()
block, everything is undefined and errors will be thrown. I'm confused by this... Shouldn't at least some of them (constant
is the first to come to mind) be immutable objects?
Is this the expected behavior or am I doing something wrong?