Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Classes and Prototype Methods

Thursday, December 23, 2021

JavaScript Classes and Prototype Methods

A JavaScript class is different than classes in other languages like C# or Java.  A class in JavaScript means that the objects inherit properties from the same prototype object.  It is defined by using the function to initialize and return a new object.

First thing we have to do define the prototype object by creating a function constructor that initializes and creates a new object.  Let's say we have a bank account first we will create a constructor that will create a new account object.  We then define the class methods by defining the prototype methods.  After we do that all the new objects that created will inherit those prototype methods. Like the code below.


    <script>
    <script>
        function Account(balance, type) {
            this.balance = balance;
            this.type = type;
        }

        Account.prototype.deposit = function (d) {

            this.balance = this.balance + d;
            return this.balance;
        }

        Account.prototype.withdrawal = function (w) {
            this.balance = this.balance - w;
            return this.balance;
        }

        var a = new Account(100, "Checking");
        var bonus = 10;
        var fine = 20;

        console.log("Deposit " + bonus + " " + a.deposit(bonus));
        console.log("Withdrawal " + fine + " " +  a.withdrawal(fine))
        console.log("Account type " + a.type);
    </script>














The code above defines an account class using a function, and then we define the methods or behavior of the class with prototype methods.  After we opened the account we got a bonus of ten      dollars.  So we call the deposit the ten dollars to the account by calling the method deposit that belongs to the prototype of the class, which we inherit when we create the account object.

Our new balance is 110 dollars.  As you can see we set the objects property to the new balance.  When we pay the fine we subtract twenty dollars from the new balance.  All of this happens within the account object because we defined and initialized the account object with a constructor and use prototype methods.  We can create other objects with the same properties and behaviors by using the account class.  JavaScript does not have a class, but it can simulate classes with prototypes and functions.

5 comments:

  1. The advanced compensated media applications are customized specifically to your business as well as designed to maximize your visibility as well as usefulness on each applicable advertising platform.
    best UX designers

    ReplyDelete
  2. I am so delighted I found your weblog, I really found you by accident,
    And would just like to say thank you for a fantastic post and a all round entertaining blog (I also love the theme/design), DMV & DVLA FAKE DRIVERS LICENSE THAT WORKS I don't have time to go through it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.

    Y’all don’t forget to join this DMV EXPERTS TELEGRAM GROUP for more information about new drivers license and updated materials being used along the production process of the DL . You can also take advantage to learn and meet many Experts who will guide you on numerous techniques for anyone who love hacking and don’t know how to go about it .

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Yes, Java Script is a very advanced programming language. And it has a lot of prototype functions. I am developing software on Javascript, which will provide many services like assignment writing services, thesis writing services, and Cheap Assignment Writing Service.And many people have benefited from this software for assignment quizzes and research as related.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete

Search This Blog