Tuesday, September 10, 2013

JavaScript : OOP part 2

if (!theFrontier.final()) {theFrontier.current(true, 'part 2');}

Why don't we see object oriented javascript much as we should?

I don't know why we don't see it as much as we should. If I am speculating, (which I am), it's due in part to the way JavaScript was created, and also in part to Microsoft's refusal (until somewhat recently) to implement JavaScript spec in IE. Internet Explorer was the browser of choice in the early(ish) days of the web. More specifically, IE was the browser of choice when the web began to gain traction as a viable business tool. During that time, Microsoft created a scenario that allowed desktop developers to not have to know much about web development to do it. As such, many of the developers of web content and web applications did not actually have to care or worry about JavaScript development as web forms developers. And I think it's probably fair to say most of us were grateful to Microsoft for it at that time.

Now, however, we know better. Developers are rushing to catch up and keep up with the current incarnation of the www and current web application development practices. Sadly, despite the popularity of asp.net MVC (MVC in general actually), many developers are still averse to JavaScript, and are doing things like creating type-safe JavaScript abstractions that allow a developer to write type-safe code that produces JavaScript, or learning to use these types of tools so as to avoid learning JavaScript. This is silly.

So again, why don't we see object oriented javascript much as we should? I think the answer is twofold. On the one hand - we should begin to see it everywhere because we should strive to implement it in every situation where it will offer us benefit. On the other hand - we see it more than we realize; it's already out there, at large, doing our bidding. Did you recognize it?

jQuery is object oriented.


Take the following plugin:
If you were to debug the example and view the $ object, you might think you would find all the jQuery functions and properties that you know and love. This is not so. Accessing $ returns an initialized object. Accessing $ actually initializes and returns $.fn, and the $.fn object (the object that is created/accessed via $) is jQuery. To state it more simply, $.fn is the object that actually holds all the configuration, settings, functions, methods and other magic that comprise the most widely used javascript library on the net.

In the above fiddle, $.fn is the object being extended. Any time you add a plugin to jQuery, you are extending the $.fn by adding a member/property to it. So - a jQuery plugin simply allows a developer to use jQuery while extending jQuery. Pretty smart, right?

Prototype js is object oriented.


Prototype was built with object oriented programming in mind. Object oriented javascript is Prototype's WHOLE thing. It aims to make OOP in javascript simpler by allowing the developer to work with expressiveness that better mirrors the typesafe object oriented languages most developers are already familiar with.

It's silly to explain how Prototype is object oriented because it has to be. Prototype abstracts away all of the javascript mechanics of object oriented programming in a way intended to make things easier. If you are coming from something like Java or C# and don't have a lot of javascript knowledge/experience, Prototype js makes the task of writing object oriented javascript a little easier to tackle.
Here's an example:

Angular js is object oriented.


If you have worked with Angular js at all, you probably realize that controllers kinda feel like objects. They are, but not like you might think they are. From the developer's perspective, controllers are pretty much the thing through which we interact with scope. We're not setting or interacting properties of a controller as much as using a controller to interact with properties of scope.

Regardless - AngularJs is a dependency injected, fully object oriented client side javascript framework. Nuff said.

So on and so forth...


If you can name a popular javascript library, you can name an object oriented javascript library. The best javascript libraries are always going to be written in good javascript.





No comments:

Post a Comment