Web
Analytics
Java Script Classes and Objects | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Now I am reveling concept of classes and objects into Java Script.

JavaScript is a class-less language. Everything is an object. And when it comes to inheritance, objects inherit from objects, not classes from classes as in the classical languages.

We can create classes and objects in three ways , One of the very popular way to define javascript is by using functions.

In JavaScript, all values, except primitive values, are objects

Using a function

We declare a simple JavaScript function and then create an object by using the new keyword. To define class properties and methods for an object created using function(), you use the this keyword.

<!DOCTYPE html>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

    <title></title>

</head>

<body>

    <script>

        function myAccount(id,name,balance, deposit)

        {

            if(id==undefined & name==undefined & balance==undefined )

            {

                return false;

            }

            this.id = id;

            this.name = name;

            this.balance = balance;

            this.deposit = deposit;

            //Function of class itself

            this.showaccountdetails=function()

            {

                return ‘ID= ‘ + this.id + ‘ Name=’ + this.name + ‘ Balanace=’ + this.balance;

            }

            //Function of class itself

            this.showdeposit=function()

            {

                return ‘Deposit=’ + this.deposit;

            }

            //Pass the address of outer function into current class

            this.completedetails = completedetails;

        }

        function completedetails()

        {

            return ‘ID= ‘ + this.id + ‘ Name=’ + this.name + ‘ Balanace=’ + this.balance+’Deposit=’+this.deposit;

        }

        //Calling default constructor , which does not return anything.

        //var accoundetails = new myAccount();

        //output=Nothing

        //Create object with constructor

        var accoundetails = new myAccount(1, ‘Yogesh’, 10000, 20000);

        alert(accoundetails.showaccountdetails());

// alert(accoundetails.showdeposit());

        //call outer function

     //alert(accoundetails.completedetails());

    </script>

</body>

</html>