Object-oriented (OO) languages usually are recognized through their use of classes for creating various objects which have similar 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 method is recognized by a name 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 the 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 displays 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 object in the previous example, with all those same properties and methods added. These properties are all created with specific 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: Encapsulation is the capability of storing related information, whether data or methods, mutually in a single object.
  • Aggregation: Aggregation 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: Polymorphism 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 a method of the object, or 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 used within the object's methods but can also be globally visible variables 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;


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram