JavaScript/Object-based programming

Previous: Anonymous functions Index Next: Objects

Object-Based ProgrammingEdit

JavaScript supports object-based program design. (It also supports imperative and functional program designs.[1])

The purpose of an object-based design is to allow the components to be as modular as possible. In particular, when a new object type is created, it is expected that it should work without problem when placed in a different environment or new programming project. The benefits of this approach are a shorter development time and easier debugging, because you're re-using program code that has already been proven. This 'black box' approach means that data goes into the Object and other data comes out of the Object, but what goes on inside isn't something you need to concern yourself with.

In contrast to object-oriented programming that uses classes, JavaScript does not have any; it uses objects to represent complex data types. Some objects are created manually, while others are built-in to the language itself, like the Date object. Objects are small structures of data with their own fields and functions to access or modify these fields. A benefit of objects is that they use a "black box" approach where the internal variables are protected from most forms of outside interference. This helps to prevent them from being altered by program code outside of the function or object, which provides a better integrity for the data itself.

Unlike other programming languages, JavaScript does not provide implicit protection levels on members of an object. In particular, JavaScript uses a prototypical form of objects, which can still inherit from parent classes. Although JavaScript is only an object-based and not an object-oriented language, most design patterns can still apply within the language as long as there is no attempt to directly access the object's internal state (for example, using an object's methods.)

As with many other programming languages, objects in JavaScript have their fields and functions referenced by the dot (.) between the object and field name.

The philosophy of object-based programming says that program code should be as modular as possible. Once you've written and tested a function, it should be possible to slot it into any program or script needing that kind of functionality and just expect it to work, because it's already been tried and tested on an earlier project.

In JavaScript, you can make your own objects (like the document object) and even define your own variable types. Although JavaScript allows the creation of objects, access protection is not directly available; as such, it is possible to bypass this intent by directly accessing fields or methods. To minimize the impact of these issues, you may want to ensure that methods are properly described and cover known situations of use, and to avoid directly accessing methods or fields that are designed to hold the internal state of the object.

Further readingEdit

Previous: Anonymous functions Index Next: Objects
  1. An earlier note on this page said: 'Wikipedia, "JavaScript" says: "JavaScript is classified as a prototype-based scripting language with dynamic typing and first-class functions. This mix of features makes it a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles."' (That note was added in 2015.)