JavaScript Programming Paradigm

Is JavaScript OO, OOP, or OOD?

Overview Object Oriented (OO) Object Oriented Programming (OOP) Encapsulation Inheritance Polymorphism Object Oriented Design (OOD) Summary

Overview

This short article addresses the question of the JavaScript programming paradigm. This Web page discusses whether or not JavaScript is simply Object Oriented (OO), a fully Object Oriented Programming Language (OOP), or an Object Oriented Design Language (OOD). Apparently there's some debate about whether or not JavaScript is an OO, OOP, or an OOD, language.

This Web page covers the differences and similarities between OO, OOP and OOD. This article explains how OO, OOP and OOD, apply to JavaScript.

Object Oriented (OO)

An OO language is an object oriented language. That means developers can define an object, then use that object repeatedly in the same or multiple programs. An object maintains it's own properties and functions. Use copies of the object in a program with the new keyword. Copies of an object are called instances of the object. Some consider definition of a distinct object, as the definition of a class.

JavaScript is at least an OO language. The Four Sided Shape with Boundary Detection tutorial demonstrates creation of re-usable objects with their own properties and functions.

Object Oriented Programming (OOP)

An OOP language includes objects, encapsulation, inheritance, and polymorphism. This short article isn't covering the details of OOP here. However, this lesson briefly describes encapsulation, inheritance, and polymorphism. Encapsulation means fields and functions in an instance of a class can be hidden from access, outside the class. Inheritance means a child class can use fields and functions from a parent class. Polymorphism means a parent class can behave like a child class.

Encapsulation

Encapsulation allows the programmer to declare a class named for example; MyClass, containing a hidden variable named myHiddenVariable. A JavaScript file creates an instance of MyClass with var mc = new MyClass(). If myHiddenVariable is truly hidden, then the line mc.myHiddenVariable = x; would generate an error. Currently that's not the case with JavaScript.

Inheritance

Inheritance allows the programmer to declare a class named for example; ParentClass, with the public function parentFunction(). The programmer declares a child class called ChildClass. With appropriate keywords ChildClass can automatically receive all the public functionality of ParentClass. A JavaScript file creates an instance of ChildClass with var cc = new ChildClass(). With inheritance a call to cc.parentFunction(), would execute parentFunction().

Polymorphism

Polymorphism can be described by extending the explanation of Inheritance, above. Assume the programmer adds a function to ChildClass called childFunction(). The programmer creates an instance of ParentClass with pc = new ParentClass(). The programmer has already created an instance of ChildClass with var cc = new ChildClass(). The line of code pc = cc; assigns an instance of the ChildClass to the ParentClass. With Polymorphism pc.childFunction() and pc.parentFunction() would execute both functions correctly.

Object Oriented Design (OOD)

An OOD language includes objects, yet omits some of the other aspects of OOP. At this point in time it appears that JavaScript is an OOD.

For example the Four Sided Shape with Boundary Detection tutorial explains how to prepare separate objects with JavaScript. The e-book WebGL Textures & Vertices demonstrates how to prepare JavaScript objects with both properties and functions. However encapsulation is not available with JavaScript at this time. Any function or property of a JavaScript object remains public. In other words outside code can access both properties and functions (or methods) declared in a JavaScript object.

There are various work arounds to make JavaScript behave like OOP. Yet those work arounds are not part of the JavaScript standard, neither do they use common forms of OOP syntax. Hopefully as the future moves more toward JavaScript with HTML5, classical OOP syntax and functionality, will become the standard.

Summary

This short article addressed the question of the JavaScript programming paradigm. This Web page discussed whether or not JavaScript is simply Object Oriented (OO), a fully Object Oriented Programming Language (OOP), or an Object Oriented Design Language (OOD). Apparently there's some debate about whether or not JavaScript is an OO, OOP, or an OOD, language.

This Web page covered the differences and similarities between OO, OOP and OOD. This article explained how OO, OOP and OOD, apply to JavaScript.

Have fun and love learning!

WebGL Textures and Vertices eBook Screen Shots
Copyright © 2015 Seven Thunder Software. All Rights Reserved.