~ 4 min
Rethinking about inheritance in AngularJS 1.5
In that example I used it to implement the observer pattern in different services without having to define the same functions again and again.
I think it is a common problem when dealing with the (now old) ECMAScript 5, as long as you want to avoid code repetition and structure your code in a similar fashion as you would with a pure object oriented language, such as Java or C#.
After thinking about it and trying out many experiments, I decided it’s not really worth it, since it complicates your code and makes it more difficult to read and maintain. But of course, repeating the code is no solution either. So maybe we can find an easier solution.
I think we shouldn’t fight against the language we’re using: it’s very tiring and at the end of the day it’s going to make our code worse.
Trying to do something the language was not designed for is somehow fighting against it. Instead of that, a better solution would be using the native characteristics of that language to fulfill our goals.
Here I come with an alternative solution that worked for me with AngularJS services. The idea is a service with a toolset of constructors, implementing the code of the objects we may want to use again and again. Something like a factory.
Depending on how many constructors you want to define, you can have different factory services.
Now, if you want to create a new service that implements the functions defined in
Observer, you can do something like this:
This way, you will have a property in the
NewService that’s called
observable, on which you can call the functions of an
Observer object that only exists in
You have not repeated the code of the functions defined in
Observable and the code is easier to understand and maintain.
What do you think about this approach? Would you prefer the other one? Do you see any potential problems when doing something like this?
Let me know!