Objects in JavaScript

JavaScript is an Object Oriented Programming language (Object Oriented Programming). A Program Language can be called object-oriented if it provides four basic capabilities to the programmer.

JavaScript is an Object Oriented Programming language (Object Oriented Programming). A Program Language can be called object-oriented if it provides four basic capabilities to the programmer:

  1. Encapsulation - The ability to store related information, data or methods, together in an object.
  2. Aggregation - The ability to store an object inside another object.
  3. Inheritance - The ability of a class to rely on another class (or several classes) for some of its properties and methods.
  4. Polymorphism - The ability to write a function or method that works in a variety of different ways.

Objects include attributes. If an attribute contains a function, it is said to be a method of the object, otherwise the attribute is considered an attribute.

Properties of the object

Object properties can be any of the three primitive data types, or in abstract data types (abstract), such as other objects. Object properties are often variables that are used within object methods, but can also be visible global variables that are used throughout the site.

The syntax for adding an attribute to an object is:

 objectName . objectProperty = propertyValue ; 

For example - The following code receives the document title by using the " title " attribute of the Document object:

 var str = document . title ; 

Object method

Methods are functions that instruct the object to do something or instruct something to be done to it. There is a small difference between a function and a method: a function is an independent unit of commands and a method is attached to an object and can be referenced by the this keyword.

Methods are useful for everything from displaying the content of on-screen objects to performing complex operations on a group of internal properties and parameters.

Example - The 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" ); 

User defined objects

All user objects define themselves (User-defined Objects) and the available object is a child of an object called Object .

New operator

The new operator is used to create an object. To create an object, the new operator is followed by the constructor method.

In the following example, constructor methods are Object (), Array (), and Date (). These constructors are built-in functions in JavaScript.

 var employee = new Object (); var books = new Array ( "C++" , "Perl" , "Java" ); var day = new Date ( "August 15, 1947" ); 

Object () Constructor

A constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object () to build an object. The return value of Object () constructor is assigned to a variable.

This variable contains a reference to the new object. Attributes assigned to the object are not variables and are not defined with the var keyword.

Example 1

Try the following example. It illustrates how to create an Object.

 User-defined objects  type = "text/javascript" > var book = new Object (); // Create the object book . subject = "Perl" ; // Assign properties to the object book . author = "Mohtashim" ;  type = "text/javascript" > document . write ( "Book name is : " + book . subject + "
" ); document . write ( "Book author is : " + book . author + "
" );

Result

 

 Book name is: Perl 
Book author is: Mohtashim

Example 2

This example illustrates how to create an object with a User-Defined Function. Here, this keyword is used to refer to an object that has been passed to a function.

 User-defined objects  type = "text/javascript" > function book ( title , author ){ this . title = title ; this . author = author ; }  type = "text/javascript" > var myBook = new book ( "Perl" , "Mohtashim" ); document . write ( "Book title is : " + myBook . title + "
" ); document . write ( "Book author is : " + myBook . author + "
" );

Result

 

 Book title is: Perl 
Book author is: Mohtashim

Define a method for an object

The previous example illustrates how the constructor creates an object and assigns properties. But we need to finish defining an object by assigning methods to it.

For example

Try the following example. It shows how to add a function with an object.

 User-defined objects  type = "text/javascript" > // Define a function which will work as a method function addPrice ( amount ){ this . price = amount ; } function book ( title , author ){ this . title = title ; this . author = author ; this . addPrice = addPrice ; // Assign that method as property. }  type = "text/javascript" > var myBook = new book ( "Perl" , "Mohtashim" ); myBook . addPrice ( 100 ); document . write ( "Book title is : " + myBook . title + "
" ); document . write ( "Book author is : " + myBook . author + "
" ); document . write ( "Book price is : " + myBook . price + "
" );

Result

 

 Book title is: Perl 
Book author is: Mohtashim
Book price is: 100

Keyword 'with'

The keyword 'with' is used as a shorthand to reference the properties or methods of the object.

The object defined as a parameter to with becomes the default object for the duration of the block that follows. Properties and methods for the object can be used without naming the object.

Syntax

The syntax for with object is as follows:

 with ( object ){ properties used without the object name and dot } 

For example

Try the following example:

 User-defined objects  type = "text/javascript" > // Define a function which will work as a method function addPrice ( amount ){ with ( this ){ price = amount ; } } function book ( title , author ){ this . title = title ; this . author = author ; this . price = 0 ; this . addPrice = addPrice ; // Assign that method as property. }  type = "text/javascript" > var myBook = new book ( "Perl" , "Mohtashim" ); myBook . addPrice ( 100 ); document . write ( "Book title is : " + myBook . title + "
" ); document . write ( "Book author is : " + myBook . author + "
" ); document . write ( "Book price is : " + myBook . price + "
" );

Result

 

 Book title is: Perl 
Book author is: Mohtashim
Book price is: 100

JavaScript objects available

JavaScript has some objects available (built-in or native objects). These objects are accessible from anywhere in your program and will work in a similar way to any object running on any operating system.

Below is a list of all Native Objects JavaScript:

  1. JavaScript - Object Number
  2. JavaScript - Boolean object
  3. JavaScript - String object
  4. JavaScript - Array Object
  5. JavaScript - Date object
  6. JavaScript - Math object
  7. JavaScript - RegExp Object

According to Tutorialspoint

Previous lesson: Print pages in JavaScript

Next lesson: Object Number in JavaScript

4 ★ | 1 Vote