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.

6 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. This comment has been removed by the author.

    ReplyDelete
  3. 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
  4. This comment has been removed by the author.

    ReplyDelete
  5. JavaScript Classes and Prototype Methods are a game-changer, bringing elegance and structure to the language. Classes provide a clean and intuitive way to define object blueprints, promoting code organization and reusability. The introduction of syntactic sugar for class syntax simplifies complex code, making it more readable and maintainable. Moreover, prototype methods enhance the language's flexibility by allowing the addition of methods to existing object instances. This dynamic approach fosters efficient coding practices, enabling developers to extend functionality seamlessly. Embracing these features not only streamlines development but also contributes to building scalable and modular applications. JavaScript's evolution with Classes and Prototype Methods showcases a commitment to enhancing developer experience, empowering us to create robust and sophisticated solutions with confidence and ease.

    ReplyDelete
  6. Hey TechJunkies! I've been exploring ways to make JavaScript classes and prototype methods more visually engaging. One resource I stumbled upon is Depositphotos, where you can find amazing neuron images. Incorporating visuals can enhance the learning experience and make complex concepts more digestible. In response to the vibrant discussion on JavaScript classes and prototype methods, I'd love to hear your thoughts on combining code with eye-catching visuals. How do you think incorporating images like neurons could improve the understanding of these concepts? Let's brainstorm together!

    ReplyDelete

Search This Blog