Comments (5)
My $0.02. The rule of thumb in Object Orientation ( dating to the 1980s) is "favor composition over inheritance". That advice tilts toward injected services for common functionality, perhaps with a light weight prototype to do some internal service orchestration.
Deep inheritance chains are a code smell, whether in static or dynamic languages, class or prototypal inheritance.
from angular-styleguide.
If the code is reusable, shouldn't that code be in a service of its own?
from angular-styleguide.
For services I guess you're right but for controllers? Or if I feel a controller should be inherited, I'm doing something wrong?
from angular-styleguide.
It's not wrong or right, it's more about what you are trying to accomplish and finding the right tool for that. So let's take as step back and think about what prototypal inheritance is good for. Often it helps when you have lots of instances of the same object and you want to share the functions. If you create 1000 customer objects and they all share some functions such as checkCredit, it would be nice not to use memory for 1000 functions. All 1000 object instances could share that checkCredit function. But are you going to create lots of controllers where this would be super helpful? Where is it valuable? 5, 10, 100?
But you might have code that every controller seems to have, that's when I drop that code into a service/factory. Could I use prototypal inheritance? Sure, but factories are just as good and simple to test.
I'm not trying to way you either way, just give you some food for thought. So go with your instincts on this.
from angular-styleguide.
@johnpapa - What do you think for this article: Object-oriented AngularJS Services ?
This article follow the guide suggestions ?
from angular-styleguide.
Related Issues (20)
- Y022 - Naming Conventions For Functions Inside IIFE HOT 1
- JS Hint configuration code is out of date.
- Best way for long controller $inject HOT 7
- Can you give simple Hello World Program? HOT 2
- dataService calls when inside page controllers vs component/directive controllers. HOT 2
- Best way for required directive attribute HOT 2
- NetBeans templates, redux? HOT 1
- Best Practices for `templateUrl` and `ngInclude` URLs HOT 3
- Arrays under scope.vm of directive undefined HOT 7
- Proposal: change from "do" to "consider" adding 'Service' suffix to Service (02-04) classes because it doesn't improve clarity or add value HOT 7
- Where is the best location to place HttpInterceptors?
- Why styleguide suggest use class instead of interface? HOT 3
- A1 - How to pass object received with $routeProvider to other route HOT 1
- Style 03-05 missing! HOT 1
- Angular 5 style guide? HOT 1
- Confusing Information on SharedModule and CoreModule HOT 3
- Should "public" always be omitted in TypeScript? HOT 1
- Private variable - leading underscore HOT 7
- Jasmine Test case failing HOT 1
- AngularJS-UnitTesting | Weather App | Karma
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-styleguide.