Skip to content

Instantly share code, notes, and snippets.

@cerebrl
Created November 25, 2013 07:18
Show Gist options
  • Save cerebrl/7637502 to your computer and use it in GitHub Desktop.
Save cerebrl/7637502 to your computer and use it in GitHub Desktop.
Factories and Services in Angular

Factories, and services in angular are singleton by definition. If you want to create a new instance you could always just do something like:

module.factory('myIntanceCreator', function(){
      return {
           create: function(){
                // create your instance here
            }
      };

});

Keep in mind that, contrary to popular belief you don’t need ‘class’ for true object-oriented-programming. If you get a chance, take a look at this talk:

http://www.infoq.com/presentations/It-Is-Possible-to-Do-OOP-in-Java

One of it’s main points is that Javascript’s closures are actually offer a much purer form of encapsulation and OOP than even other class-based OOP languages.

In a nutshell, variables and functions defined inside of a closure are strictly private, while variables or functions that are attached to via this.myVariable = value are public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment