It is an Object-Oriented Programming (OOP) language. A programming language can be called object-oriented if it provides four basic capabilities to developers.
(i) Encapsulation
(ii) Aggregation
(iii) Inheritance
(iv) Polymorphism
Encapsulation
The capability to store related information, whether data or methods, together in an object.
Aggregation
The capability to store one object inside another object.
Inheritance
The capability of a class to rely upon another class (or number of classes) for some of its properties and methods.
Polymorphism
The capability to write one function or method that works in a variety of different ways. Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object; otherwise, the attribute is considered a property.
A good Example from our real life to understand an object. As- a car
A car has “properties” like weight and color, and “methods (functions)” like start and stop:
Properties- car.name=Alto, car.model = K10, car.color = white, car.type = hatchback
Methods- car.start(), car.accelerate(), car.brake(), car.stop()
Creation of objects
There are two ways to create an object in JavaScript: as-
(i) Literal
var object = {};
(ii) object-oriented
var object = new Object();
Object Properties
Object properties can be any of the three primitive data types, or any of the abstract data types, such as another object. Object properties are usually variables that are used internally in the object's methods, but can also be globally visible variables that are used throughout the page.
The syntax for adding a property to an object is:
objectName.objectProperty = propertyValue;
Example: The following code gets the document title using the "title" property of the document object.
var str = document.title;
Object Methods-
Methods are the functions that let the object do something or let something be done to it. There is a small difference between a function and a method – at a function is a standalone unit of statements and a method is attached to an object and can be referenced by the this keyword.
Methods are useful for everything from displaying the contents of the object to the screen to performing complex mathematical operations on a group of local properties and parameters.
Example: Following is a simple example to show how to use the write() method of document object to write any content on the document.
document.write (“This is test”);
Note: We will discuss objects in detail in next unit