Advanced JavaScript or Object-Oriented JavaScript.

Working with JavaScript Objects

Objects

In JavaScript, an object is a standalone entity, and is a container for named values called properties or methods. In real life, a car is an object. A car has properties like make, colour, and style, and methods like start and stop.

Object Properties

Similarly, a JavaScript object has properties associated with it. We can define the object 'car' as follows:

          var car = {Make:"Ferrari", "Colour:Red", Style="Sport}

The name:value pairs in JavaScript objects are called properties.

Object Methods

A method is a function stored as a property. Methods are actions that can be performed on objects. Methods are stored in properties as function definitions.

          var person = {firstName:"John", "lastName:Smith", id="1020", fullName : function() { return this.firstName + " " + this.lastName; } }

In the example above, 'this' is the person object that "owns" the fullName function, so it follows that, this.firstName means the firstName property of this object.

You access an object method with the following syntax: objectName.methodName() for example, alert(person.fullName());

The Fundamental Concepts - Encapsulation, Abstraction, Inheritance and Polymorphism

Encapsulation describes the idea of bundling data and methods that work on that data within one unit, say, a class. It wraps the data and code that operates on the data (methods) into a single entity, preventing direct (unauthorized) access to that entity. Data can only be accessed by public methods configured to access bundled (say, getter and setter) methods thus making the private fields and their implementation hidden for outside classes.

The Reason for Encapsulation:

   • It improves maintainability and flexibility and re-usability
   • Class variables can be made read-only (if you omit the set method), or write-only (if you omit the get method)
   • The programmer can change one part of the code without affecting other parts
   • Increased security of data
   • Variables and their implementation is hidden for outside classes.
 

Abstraction means to perceive an entity in a system or context from a particular perspective. We remove unnecessary details and only focus on aspects that are necessary to that context or system under consideration.

The Reason for Abstraction:

   • To achieve security by hiding certain details
   • It's easier to maintain, read and work on the code
   • It helps to deal with a complex system by concentrating on the essential features only

JavaScript doesn't have the abstract class and interface features normally directly used in abstraction. However, abstract classes with interfaces are implemented indirectly by JavaScript Object.create methods and prototypes. Abstraction and encapsulation are complementary concepts.

 

Inheritance Inheritance allows programmers to create classes that are built upon existing classes and this makes it possible for a class to inherit attributes and methods of the parent class. JavaScript does not have classes like other languages. It uses the concept of prototypes for inheritance. For inheritance to work, the objects need to have characteristics in common with each other.

The Reason for Inheritance

   • It makes a specific section of a project's code reusable.
   • It can apply the same class and methods to different data types.
   • It makes global changes to derived classes by changing a base class.

Inheritance is most appropriate when the hierarchy represents an 'is-a' (Robert is a name) relationship and not a 'has-a' (the house has a room) relationship and, the class hierarchy continues to be reasonably shallow.

 

Polymorphism in Object-Oriented Programming is the ability to create a variable, a function, or an object that has more than one form.

The programmer does not have to know the exact type of the object in advance, and so the exact behaviour is determined at run-time. This is what we call late binding or dynamic binding. Requirements are such that, there must be properties with the same name and the same parameter sets in all the superclasses, subclasses and interfaces.

The Reason for Polymorphism

   • It enables objects belonging to different types to respond to methods, fields, or property calls of the same name, each one according to an appropriate type-specific behaviour.

Say there is a base class named Animals from which the subclasses Horse, Fish and Bird are derived. Also assume that the Animals class has a function named Move, which is inherited by all subclasses mentioned. With polymorphism, each subclass may have its own way of implementing the function.

 

Resource Management

Computer programs may manage their own resources by using inherent features of programming languages or may elect to manage them by an operating system, virtual machine or another program.

Resource management, is sometimes referred to as resource tracking, and consists of cleaning up resource leaks: terminating access to resources that have been acquired but not released after use. This is known as reclaiming resources and is analogous attempts to reclaim occupied objects that are no longer being used. On many systems the operating system reclaims resources after the process makes the exit system call.

The act of refusing to release a resource when a process has finished using it is known as a resource leak and is an issue in sequential computing (executing a single sequence of instructions). Multiple processes wish to access a limited resource can be an issue in concurrent computing, and is known as resource contention.

Resource management seeks to control access in order to prevent both of these situations.



copyright © Paul A. E. Sheridan 2019