what is Object Oriented Programming ??

do you know what is Object Oriented Programming ?? if you don’t know don’t worry we will help you. in this Object-Oriented Programming article, So Without Wasting Time Let’s Know About The Object-Oriented Programming.

Object-Oriented Programming:

JavaScript is an Object Oriented Programming (OOP) language. An OOP language allows you to define your own objects and make your own variable types. Note that an object is just a special kind of data. An object has properties and methods.

Properties:

Properties are the values associated with an object.

In the following example, we are using the length property of the String object to return the number of characters in a string:

<script type= “text/javascript”>
var txt= “Hello World!”;
document.write(txt.length);
</script>

The output of the code above will be :

12

Methods:

Methods are actions that can be performed on objects. In the following example, we are using the toUpperCase( ) method of the String object to display a text in uppercase letters:

<script type= “text/javascript”>
var str= “Hello world!”;
document.write(str.toUpperCase( ));
</script>

The output of the code above will be:

HELLO WORLD!

JavaScript String Object:

The String object is used to manipulate a stored piece of text.

String object:

The String object is used to manipulate a stored piece of text.

Examples of use:

The following example uses the length property of the String object to find the length of a string:

var txt= “Hello World!”;

document.write(txt.length);

The code above will result in the following output:

12

The following example uses the to UpperCase( ) method of the String object to convert a string to uppercase letters:

var txt= “Hello world!”;

document.write(txt.toUpperCase( ));

The code above will result in the following output:

HELLO WORLD!

JavaScript Date Object:

Create a Date Object:

* The Date object is used to work with dates and times.

* Date objects are created with the Date( ) constructor.

There are four ways of instantiating a date:

new Date( ) // current date and time

New Date(milliseconds) //milliseconds since 1970/01/01

New Date(dataString)

New Date(year, month, day, hours, minutes, seconds, milliseconds)

Most parameters above are optional. Not specifying, causes 0 to be passed in. Once a Date object is created, a number of methods allow you to operate on it. Most methods allow you to get and set the year, month, day, hour, minute, second, and milliseconds of the object, using either local time or UTC (universal, or GMT) time.

All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.

Some examples of instantiating a date:

today= new Date( )

d1= new Date(“October 13, 1975, 11:13:00”)

d2= new Date(79,5,24)

d3= new Date(79,5,24,11,33,0)

Set Dates:

We can easily manipulate the date by using the methods available for the Date object.

In the example below we set a Date object to a specific date (15th November 2020):

var myDate= new Date( );

myDate.setFullYear(2020,11,15);

And in the following example we set a Date object to be 5 days into the future:

var myDate= new Date( );

myDate.setDate(myDate.getDate( )+5);

Note: If adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!

JavaScript Array Object:

The Array object is used to store multiple values in a single variable.

What is an Array?

An array is a special variable, which can hold more than one value, at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variable could look like this:

cars1= “Saab”;

cars2= “Volvo”;

cars3= “BMW”;

Create an Array:

An array can be defined in three ways:

The following code creates an Array object called myCars:

  1. var myCars= new Array( ); // regular array (add an optional integer

myCars[0]= “Saab”;           // argument to control array’s size)

myCars[1]= “Volvo”;

myCars[2]= “BMW”;

  1. var myCars= new Array(“Saab”, “Volvo”, “BMW”); //condensed array
  2. var myCars= [“Saab”, “Volvo”, “BMW”]; //literal array

Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.

Access an Array:

You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.

The following code line:

document.write(myCars[0]);

will result in the following output:

Saab

Modify Values in an Array:

To modify a  value in an existing array, just add a new value to the array with a specified index number:

myCars[0]= “Opel”;

Now, the following code line:

document.write(myCars[0]);

will result in the following output:

Opel

JavaScript Boolean Object:

The Boolean object is used to convert a non-Boolean value to a Boolean value to a Boolean value (true or False).

Create a Boolean Object:

The Boolean object represents two values: “true” or “false”.

The following code creates a Boolean object called myBoolean:

var myBoolean= new Boolean( );

Note: If the Boolean object has no initial value or if it is 0, -0, null, “”, false understand, or NaN, the object is set to false. Otherwise it is true (even with the string “false”)!

All the following lines of code create Boolean objects with an initial value of false:

var myBoolean= new Boolean( );
var myBoolean= new Boolean(0);
var myBoolean= new Boolean(null);
var myBoolean= new Boolean(“”);
var myBoolean= new Boolean(false);
var myBoolean= new Boolean(NaN);
And all the following lines of code create Boolean objects with an initial value of true:
var myBoolean= new Boolean(true);
var myBoolean= new Boolean(“true”);
var myBoolean= new Boolean(“false”);
var myBoolean= new Boolean(“Richard”);

JavaScript Math Object:

The Math object allows you to perform mathematical tasks.

Math Object:

The Math object includes several mathematical constants and methods.

The Math object allows you to perform mathematical tasks.

Syntax for using properties/methods of Math:

var pi_value= Math.PI;

var sqrt_value= Math.sqrt(16);

Note: Math is not a constructor. All properties and methods of Math can be called by using Math as an object without creating it.

Mathematical Constants:

JavaScript provides eight mathematical constants that can be accessed from the Math object. These are: E, PI, square root of 2, square root of ½, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.

You may reference these constants from your JavaScript like this:

Math.E

Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E

Mathematical Methods:

In addition to the mathematical constants that can be accessed from the Math object there are also several methods available.

The following example uses the round( ) method of the Math object to round a number to the nearest integer:

document.write(Math.round(4.7));

The code above will result in the following output:

5

The following example uses the random( ) method of the Math object to return a random number between 0 and 1:

document.write(Math.random( ));

The code above can result in the following output:

0.33417166117578745

The following example uses the floor( ) and random( ) methods of the Math object to return a random number between 0 and 10:

document.write(Math.floor(Math.random( )*11);

The code above can result in the following output:

1

JavaScript RegExp Object:

“RegExp is short for regular expression.”

Complete RegExp Object Reference:

What is RegExp?

* A regular expression is an object that describes a pattern of characters.

* When you search in a text, you can use a pattern to describe what you are searching for.

* A simple pattern can be one single character.

* A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more.

* Regular expressions are used to perform powerful pattern-matching and “search-and-replace” functions on text.

Syntax:

var txt= new RegExp(pattern, modifines);

or more simply:

var txt= /pattern/modifiers;

  • pattern specifies the pattern of an expression.
  • modifiers specify if a search should be global, case-sensitive, etc.

RegExp Modifiers:

  • Modifiers are used to perform case-insensitive and global searches.
  • The i modifier is used to perform case-insensitive matching.
  • The g modifier is used to perform a global match (find all matches rather than stopping after the first match).

Example 1

Do a case-insensitive search for “w3schools” in a string:

var str= “Visit Google”;
var patt1= /google/i;

The marked text below shows where the expression gets a match:

Visit Google

Example 2

Do a global search for “is”:

var str= “Is this all there is?”;

var patt1= /is/g;

The marked text below shows where the expression gets a match:

Is this all there is?”;

Example 3

Do a global, case-insenstivie search for “is”:

var str= “Is this all there is?”;

var patt1= /is/gi;

The marked text below shows where the expression gets a match:

Is this all there is?

test( ):

The test( ) method searches a string for a specified value, and returns true or false, dependig on the result.

The following example searches a string for the character “e”:

Example:

var patt1= new RegExp(“e”);

document.write(patt1.test(“The best things in life are free”));

Since there is an “e” in the string, the output of the code above will be:

true

exec( ):

The exec( ) method searches a string for a specified value, and returns the text of the found value. If no match is found, it returns null.

The following example searches a string for the character “e”:

Example 1

var patt1= new RegExp(“e”);

document.write(patt1.exec(“The best things in life are free”));

Since there is an “e” in the string, the output of the code above will be:

e

Source: I&WT, Wikipedia

Our Suggested Article’s
ADVANTAGES AND DISADVANTAGES OF GADGETS IN OUR LIFE
What To Do If Mobile Is Stolen??
How To Choose Best Mobile Camera
How To Choose Best Processor For Mobile??
Best Top Ten Printers For Home & Office Use In 2021
How To Send Stickers In WhatsApp??
What Is Domain Name Servers? What Is Ip Address?? And Ip Address ClassesWhat Is A Modem? & Types Of ModemBest Wireless Earphones Under Rs 2000 -Tested & ReviewedConnecting To The Internet In Windows PcWhat Is Internet Definition And Evolution Of The Internet
Ten Best Bluetooth Earphones Under 1000 Rupees -Tested & ReviewedTen Best Wireless Mouse Under 1000 Rupees – Tested & ReviewedThe Best Mouse Under 500 Rupees – Tested And ReviewedBest Wireless Mouse Under 500 Rupees – Tested And Reviewed

Leave a Comment