I'm working in Angular. I've created a simple service to illustrate the issue I'm seeing. When someone calls the setProfile method on this service, my goal is to set the 'profile' to a new object literal that contains the new property values. However, when I do this it does not appear that consumers of this service can see the updates. In other words, they see the old 'profile' object, not the new one. However, if I just update the individual properties instead of assigning the 'profile' to a new object literal, they see the updates just fine. I am VERY confused.
This code works (i.e. after the setProfile method is called, consumers see the updated values):
(function () {
angular.module('myApp').factory('myService', function () {
    var profile = {
        firstName: '',
        lastName: ''
    };
    var setProfile = function (firstName, lastName) {
        profile.firstName = firstName;
        profile.lastName = lastName;
    };
    return {
        setProfile: setProfile,
        profile: profile
    }
});
}());
But this does not work (i.e. when I assign the profile to a new object literal, consumers do not see the new object):
(function () {
angular.module('myApp').factory('myService', function () {
    var profile = {
        firstName: '',
        lastName: ''
    };
    var setProfile = function (firstName, lastName) {
        profile = {
            firstName: firstName,
            lastName: lastName
        };
    };
    return {
        setProfile: setProfile,
        profile: profile
    }
});
}());
Here is an example of how the service may be used:
module.run(['$rootScope', 'currentUser', function ($rootScope, currentUser) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    debugger;
    if (!currentUser.firstName == 'test') {
        return;
    }
});
}]);
 
    