简体   繁体   English

如何在 JavaScript 中调用另一个 function 内部的 function

[英]How to call a function inside of another function in JavaScript

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,里面有一个 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 //如果应该调用 autoClicker 但它不是,则在末尾的这一行
this.autoClicker;这个.autoClicker;

It works for me.这个对我有用。 Two things:两件事情:

  1. I changed Class to class .我将Class更改为class
  2. I created an instance of the Student class.我创建了一个学生 class 的实例。

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

JavaScript is case-sensitive so class and Class are considered to be different. JavaScript区分大小写,因此classClass被认为是不同的。 The keyword is class .关键字是class Class would be an unexpected identifier at this position. Class将是此 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.可以将 class 想象成 class 实例的蓝图,类似于您对房屋的规划。 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).但是,为了能够使用 class,您需要根据所述蓝图(= 使用该计划建造实际房屋)创建一个 object(= 一个类的实例)。 You will need to use the new keyword for this.为此,您需要使用new关键字。

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 .您应该了解 object 导向编程的基本概念,如这篇文中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM