Home > Software engineering >  How to call a function inside of another function in JavaScript
How to call a function inside of another function in JavaScript

Time:01-05

I looked on whole stack overflow but unfortunately, answer of this question wasn't available so I have a class and inside I have a function:

class DonutMaker {
  constructor() {
    this.donut_creater = document.getElementById("donut_creater");
    this.donut_creater.addEventListener("click", this.donutMaker);

    this.auto_clicker = document.getElementById("auto_clicker");
    this.auto_clicker.addEventListener("click", this.autoClickerHandler);
  }

  donutMaker() {
    this.selection = document.getElementById("donut_quantity");
    this.quantity = this.selection.innerText;
    this.updated_quantity = parseInt(this.quantity);
    this.updated_quantity = this.updated_quantity   1;
    this.selection.innerText = this.updated_quantity;
  }
  autoClickerHandler = () => {
    this.selection = document.getElementById("donut_quantity");
    this.quantity = this.selection.innerText;
    this.updated_quantity = parseInt(this.quantity);
    this.new_quantity = this.updated_quantity - 1;

    if (this.updated_quantity >= 1) {
      this.selection.innerText = this.new_quantity;
      this.quantity = this.new_quantity;
      this.selection2 = document.getElementById("auto_clicker_quantity");
      this.auto_clicker_quantity = this.selection2.innerText;
      this.auto_clicker_quantity = parseInt(this.auto_clicker_quantity);
      this.auto_clicker_quantity_updated = this.auto_clicker_quantity   1;
      this.selection2.innerText = this.auto_clicker_quantity_updated;
      this.autoClicker;
    } else {
      console.log("Not Eligible");
    }
  };
  autoClicker = () => {
    console.log("Hello");
    // console.log("Auto clicker");
  };
}

let obj = new DonutMaker();

//This line at the end of if supposed to call autoClicker but it isnt
this.autoClicker;

CodePudding user response:

It works for me. Two things:

  1. I changed Class to class.
  2. I created an instance of the Student class.

    class Student{
        a(){
            this.b();
        }
        b(){
            console.log("B");
        }
    }
    var student = new Student()
    student.a(); // B

CodePudding user response:

JavaScript is case-sensitive so class and Class are considered to be different. The keyword is class. Class would be an unexpected identifier at this position.

A class can be imagined as a blueprint for an instance of that class, similar to how you have a plan for a house. However, for you to be able to work with a class you need to create an object (= an instance of a class) based on said blueprint (= building the actual house using the plan). You will need to use the new keyword for this.

On that instance you can then call the methods you have defined.

// Student class/ blueprint
class Student {
  a() {
    console.log("a");
    this.b();
  }

  b() {
    console.log("b");
  }
}

// Create an instance/ object of type Student
const myStudent = new Student();
// Now call method on that instance
myStudent.a();

You should probably get your head around the basic concepts of object oriented programming as explained in this blog post.

  • Related