JavaScript Object Oriented

Object-oriented (OO) languages usually are recognized through their use of classes for creating various objects which have the equivalent properties and methods. It is to be noted that, ECMA-Script has no concept of classes, and hence objects are different than in class-based languages.

ECMA-262 describes and classifies an object as an "unordered collection of dissimilar properties each of them having a primitive value, object, or function." Firmly speaking, this means that an object is an array of all values in no specific order. Each property and/or method is recognized by a name which is mapped to a value. For this reason, it thinks of ECMA-Script objects as hash tables i.e., nothing more than a combination of name-value pairs where the value may be data or a function. In this chapter, you will learn about JavaScript Object oriented concepts.

Understanding Objects

The simplest way to create a custom object is to create a new instance of Object and add properties and methods to it, as in the example mentioned below:

var person = new Object();
person.name = "Karlos";
person.age = 23;
person.job = "Network Engineer";
person.say_Name = function(){
alert (this.name);
};

This example creates an object called the person that has three properties which are: name, age, and job and one method (say_Name()). The say_Name() method is used to display the value of this.name, which resolves to person.name.

The previous example can be rewritten using literal object notation as follows:

var person = {
name: "Karlos",
age: 23,
job: "Network Engineer",
say_Name: function(){
alert(this.name);
}
};

The person object in the above example is equivalent to the person object in the prior example mentioned earlier, with all those same properties and methods added. These properties are all created with certain characteristics that define their behavior in JavaScript.

Since JavaScript is an object-oriented programming language and so a programming language can be called object-oriented when it provides programmers with at least four basic capabilities to develop:

  • Encapsulation: It is the capability for storing related information, whether data or methods mutually in a single object.
  • Aggregation: It is the ability to store one object inside another.
  • Inheritance: A class can depend upon another class or number of classes and inherit their variables and methods for some specific use.
  • Polymorphism: It is the potential of the concept of OOP for writing one function or method which works in a variety of different ways.

Objects are composed of attributes, and when an attribute contains a function, it is considered to be a method of the object else the attribute is considered a property.

Object Properties in JavaScript

Object properties can be any of the three basic data types or any of the abstract data types. Object properties are variables which are used within the object's methods, but as well can be globally visible variables which are used throughout the page.

The syntax for including any property to an object is:

Obj_Name . obj_Property = property_Value;

Example:

var obj1 = project.title;

Scroll Back to Top