My Angular On Inheritance — Angular is Javascript Meta

Daniel Chang
Always Be Coding
Published in
4 min readJun 7, 2017

--

This is kind of a continuation on my initial Angular vs React.

Yes! It’s the FP!

For those who have been following, sorry I’ve been missing in action. I’ve spent the last few months settling into work, relocating down to Los Angeles, and flying up and down on weekends. So, I’ve had very little bandwidth to focus on many things I would have liked to. Things are are now a bit more settled, so here’s to being able to write more consistently.

New Job — Old Angular

I recently started a new job at Science 37. It’s been a very exciting time, the team is great, and I feel like I’m learning a lot more on a wide variety of topics than I did at Hack Reactor. I mean, obviously Hack Reactor established a good base otherwise I wouldn’t be doing what I’m doing right now, but I’d like to echo a common sentiment.

You SHOULD learn more on your first X months on the job than the X months you spent in your bootcamp.

More on work later, but the take away for this post is that I’m working in Angular 1.6x.

But React?

I may still have a React bias though. Ultimately what gets me with React is that I *felt* it was pretty easy to follow data flow. In vanilla React you explicitly have to state and pass things through. It gets pretty unwieldy, and enter Redux stage right. Good news bears all around.

In all fairness, this is based off a super limited amount of time working with React and I still stand by my assessment, it would be unfair of me to not consistently assess this situation.

So Angular huh?

As I work more and more with Angular, I’m starting to appreciate somethings about it and I’m also understanding why Angular (and even Angular 2) may not win mindshare right now.

Functional Programming vs Object Oriented Programming

It ultimately boils down to the current trend towards Functional Programming, or FP as they like to say. (Insert some obscure The FP reference.) All the cool kids are doing it, so why aren’t you? Javascript is not a purely Functional Programming language, but everyone under the sun is trying to fit that paradigm onto Javascript.

The problem for Angular is that it doesn’t subscribe to Functional Programming concepts. So not only is it not the hot new kid in school, it actually refuses to adhere to the same styles as thew cool kids.

But Javascript is Object Oriented… right?

Yes, it is. I’m not arguing if Object Oriented is better than Functional or not, but the fact of the matter is Javascript is Object Oriented.

So How Does This Relate To Angular

Angular is actually pretty Object Oriented in it’s train of thought. Let’s look at the following example.

// page1.html
<div ng-controller="Ctrl1">
<ng-include="page2.html">
</div>
// page2.html
<div ng-controller="Ctrl2"> {{myVariable}} </div>
// controllers
angular.module('myModule1', []
.controller('Ctrl1', function Ctrl1() {
var self = this;
this.init = function () {
$scope.myVariable = "Hello World";
};
self.init()
})
)
angular.module('myModule2, ['myModule1']
.controller('Ctrl1', function Ctrl2() {
})
)

So what’s going on here?

We have a div element that uses ng-include to include some HTML as a child div. Each div has their own controller. Inside the child div, we are looking to a $scope.myVariable to render.

But this variable doesn’t exist in the controller for the the child div. So what happens?

We look up our inheritance chain. It turns out the parent div has a controller where myVariable is defined and thus we can render that $scope variable.

Inheritance

If this seems familiar to you, it should! This is normal object inheritance which is a core Javascript concept just repackaged!

Conclusion

It goes to show how little experience I had with Angular when I first made my initial assessment. It’s possible I actually leveraged this inheritance pattern within Angular before, just never noticed it.

First take away is to keep questioning your opinions and assessment. They should change as you get new information and have new experiences.

The second take away, and this is more of a conjecture, but if there is ever a pure Javascript movement, I argue Angular (probably current version of Angular, not Angular 1.6x) will see a resurgence in popularity. At a meta-level, I feel Angular and Javascript are pretty tied together.

Unfortunately the current paradigm (read trendiness) in Javascript programming leaves Angular kind of on the outside. Only time will tell if that changes.

Had fun reading this? Found it useful? Recommend or share on Facebook/Twitter! Check out more posts on Always Be Coding.

--

--

Code monkey always looking to learn more, avid car enthusiast celebrating #WRXmas all year long, amateur chef, professional eater.