>From OO perspective, it is a bad idea to use inheritance for code
reuse. It makes code hard to reason, because there are things you call
- coming from nowhere, I mean coming from some inheritance chain which
is only known to person writing the code. Also, it is a static
linking, so you cannot mock/replace those methods in a straightforward
way if you need it. Not everyone will agree though. Sometimes it might
be pragmatic to use inheritance, so there is no clear "STOP" sign.

In your case you have to think about $scope which is not directly
available from within your prototype methods. You can bind it to
`this` when constructing your controller and access it through it in
methods, but you have to remember to do it before calling super
constructor if the super constructor needs that scope itself.

Class inheritance is tricky in JavaScript. People do not use it often
for that reason. I am coming from Java (professionally, day by day
since 2005) and the very rare use of inheritance in JavaScript is what
I love about this language most. Maybe I am biased, because in Java
people overuse and misuse inheritance all over the place up to the
point I am sick seeing it :)

Regards,
Witold Szczerba

On Wed, Oct 1, 2014 at 9:10 AM, Maryan Bahnyuk <[email protected]> wrote:
> Hello.
>
> I have two controllers with some similar functions/methods. So I think that
> it should be a good idea to use an abstract controller, move these similar
> functions/methods to an abstract controller and to inherit these
> functions/methods in current two controllers.
>
> I.e. I'd like to do something like:
>
> function AbstractController() {
> }
> AbstractController.prototype.similarFunc1 = function() {
> };
> AbstractController.prototype.similarFunc2 = function() {
> };
>
> var Controller1 = function() {
> AbstractController.call( this );
> ...(skipped)
> };
> Controller1.prototype = Object.create( AbstractController.prototype );
> ...(skipped)
>
> var Controller2 = function() {
> AbstractController.call( this );
> ...(skipped)
> };
> Controller2.prototype = Object.create( AbstractController.prototype );
> ...(skipped)
>
> Is it a bad style or can I use such 'tricks'? :)
>
> P.S. If idea is not bad then it would be great to have such info in
> AngularJS docs. If idea is bad then it would be great to know the reason. In
> any case such info may be useful here:
> https://docs.angularjs.org/guide/controller
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to