JavaScript Object Oriented Programming - JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)

JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)

Chapter 13. JavaScript Object Oriented Programming

Congratulations on reaching this part of the book. As of now, you know the basics of JavaScript. With your current knowledge, you can now provide semi-dynamic content. With a little knowledge in server-side scripting language and MySQL, you will be capable of creating fully dynamic websites with rich web content. Sounds good, right? So what is next?

The next part that you need to know is object oriented programming. Learning this programming paradigm will allow you to create advanced, complex, and large scripts with ease and clarity. With it, you will be able to harness more than half of JavaScript’s capability.

With the knowledge of object oriented programming, you will be able to create web applications like games, eCommerce sites, and complex data manipulation scripts. Also, you will have familiarity with almost all popular programming languages available. Of course, this is not a promise or a guarantee. This book will only show you the way, and it is up to you if you are going to walk on it.

Programming Paradigms

In programming, goals can be achieved and programs can be created in multiple ways. For example, a calculator program may function all the same but can be coded differently. An addition operation in programming, or even in Mathematics, can be performed in different ways. One can do it by simply using the addition operator (1 + 1), or another can do it by using the subtraction operator and the unary negative operator (1 – (-1)).

Of course, despite achieving the same goal, programs with different source codes may have additional behaviours that may be controlled or uncontrolled. One of those behaviours is the tendency of a code using more resources than the other code, despite providing the same purpose as the other.

In creating larger and more complex program development, how you write your code will matter. Depending on how you write, your program may perform faster or slower. Your development time may become easier or harder. Your code might be easily read or difficult to comprehend.

And due to those concerns, broad programming models and paradigms were developed. Standard practices started to exist. And writing styles were created. On the flip side, due to the advancement in computing, those issues mentioned before have been reduced and might become too unnoticeable, especially for beginners.

These days, choosing the programming model, standard practice, and writing style has become more of a preference issue rather than performance. However, do still note that in bigger projects, as mentioned before, choosing the right model, styles, and practices can still provide a huge impact to you, your team, and your program.

Structured and Unstructured Programming

Since this is a book about JavaScript, a multi paradigm language, you will be provided with an introduction to object oriented programming and a brief explanation to procedural programming.

So what is object oriented programming anyway? As it name implies, OOP is a programming model that focuses on objects rather than just statements or actions. Before you have reached this chapter, you have been performing procedural programming, which heavily relies on functions.

Object oriented programming is considered as a structured paradigm. Structured contrasts with unstructured, a programming model that tends to make programmers create linear programs or codes that are usually run incrementally through line numbers and jump from one point to another using goto commands. The unstructured model is a primitive paradigm that often leads to messy codes, which is called spaghetti code due to its recursive nature.

The main difference of structured and unstructured programming is the usage of functions, procedures, or subroutines. Instead of just relying to goto, a flow control command, developers can take advantage of “grouping” certain statements and use them readily by invoking them. It eliminates the need for the parser to incrementally move through and execute unnecessary statements or make the programmer create conditional jumps to get back to the previous line where the initial goto was invoked. Of course, those can be easily remedied by using loops and other keywords. However, the exclusion of the usage of functions can prevent you to easily allow your program to perform repetition of grouped statements without rewriting and pointing your code to go back in a specific line number again and again.

Of course, unstructured programming was not a preferred choice; rather, it was a limitation of the programming languages back then. When functions were introduced, developers became more accustomed in procedural.

Yes, as its name implies, procedural programming is a paradigm that heavily relies on procedures or functions. Procedural programming is considered as a structural programming paradigm. It is also related to code block programming; however, for simplicity’s sake, the book will only cover procedural.

Procedural Programming

As mentioned a while ago, before you have reached this chapter, you were actually doing procedural and functional programming. The main aspect that made you a procedural programmer up to this point is the usage of functions.

Aside from functions, the concept of variable scoping, is also a part of procedural programming, which can be mainly attributed to structured programming.

Do note that most programming languages do offer procedural programming. This paradigm is always considered as an entry point for every aspiring programmer, unless they tackle on OOP head on or decide to explore languages that are heavily reliant on other programming paradigm.

Object Oriented Programming

Now, before anything else, do note that JavaScript is a multi paradigm language. It means that multiple programming models or styles can be used in this client-side scripting language.

JavaScript can be considered as a procedural, event driven (this will be tackled on a later chapter since management of events is crucial in JavaScript), and object oriented programming language. Nevertheless, most people will refer it as an OOP language due to its programming structure and the usage of DOM (Document Object Model), which primarily forces you to interact with objects, hence accustoms you to do OOP.

But what exactly is object oriented programming?

Primarily, OOP introduces the use of objects in programming. Objects are like variables. However, instead of storing a singular data, it can have its own functions, called methods, and variables, called properties.

Together with objects, OOP also introduces namespaces, classes, constructors, inheritance, encapsulation, abstraction, and polymorphism.