在未定义的函数类中调用函数的Javascript

在未定义的函数类中调用函数的Javascript,javascript,Javascript,我正在调用一个方法,并希望该方法获取第二个方法返回值的值,以便能够在元素中使用该变量 我总是能够通过将函数放在另一个函数中来调用函数。在使用类时,我似乎无法做到这一点 我必须使用某种回调方法吗。我是新来上课的 class Bills{ constructor(amount,payment,duedate,apr){ this.amount = amount; this.payment = payment; this.duedate = d

我正在调用一个方法,并希望该方法获取第二个方法返回值的值,以便能够在元素中使用该变量

我总是能够通过将函数放在另一个函数中来调用函数。在使用类时,我似乎无法做到这一点

我必须使用某种回调方法吗。我是新来上课的

class Bills{

    constructor(amount,payment,duedate,apr){
        this.amount = amount;
        this.payment = payment;
        this.duedate = duedate;
        this.total = total;
        this.apr = apr;

    }

  amountByMonth(billings){

//This is the function I am calling inside the function to get the value of.
      let dueDays = daysLeft(billings);

    const items = document.getElementById('bills');
    const newitem = document.createElement('ul');

    newitem.innerHTML = `
    <li>Monthly Amount Due :${billings.amount}</li>
    <li>Monthly Amount Due :${dueDays}</li>
    <li>Total On Card: ${billings.total}</li>`;


    items.appendChild(newitem);


  }

  daysLeft(billings){

let date1 = new Date();
let dueDate = new Date(billings.duedate);
let timeDiff = Math.abs(dueDate.getTime() - date1.getTime());
let diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
console.log(diffDays);

return diffDays;

  }





}// end 




document.getElementById('subBtn').addEventListener('click',valueinput);

function valueinput(e){

 let amount = document.getElementById('payment').value;
 let total  = document.getElementById('total').value;
 let duedate = document.getElementById('dues').value;


  let billings = new Bills();

  billings.amount = amount;
  billings.duedate = duedate;
  billings.total = total;



  billings.daysLeft(billings);
  billings.amountByMonth(billings);

  e.preventDefault();
}
类别账单{
建造商(金额、付款、付款日期、4月){
这个。金额=金额;
这个付款=付款;
this.duedate=duedate;
这个.总计=总计;
this.apr=apr;
}
每月金额(账单){
//这是我在函数内部调用的函数,以获取的值。
让dueDays=daysLeft(比林);
const items=document.getElementById('bills');
const newitem=document.createElement('ul');
newitem.innerHTML=`
  • 每月到期金额:${billings.Amount}
  • 每月到期金额:${dueDays}
  • 卡上总计:${billings.Total}
  • `; items.appendChild(newitem); } 戴斯勒夫特(比林斯){ 设date1=新日期(); 让dueDate=新日期(billings.dueDate); 让timeDiff=Math.abs(dueDate.getTime()-date1.getTime()); 设diffDays=Math.ceil(timeDiff/(1000*3600*24)); console.log(diffDays); 返程天数; } }//结束 document.getElementById('subBtn')。addEventListener('click',valueinput); 功能值输入(e){ let amount=document.getElementById('payment')。值; 让total=document.getElementById('total').value; 让duedate=document.getElementById('dues').value; 让账单=新账单(); billings.amount=金额; billings.duedate=duedate; billings.total=总计; 比林斯.代斯勒夫特(比林斯); billings.amountByMonth(billings); e、 预防默认值(); }
    您需要通过使用
    this
    ,明确您正在调用同一类的另一个方法,而不是不同的函数:

    当您声明一个没有function关键字的类方法时,基本上是将其声明为类对象的函数属性。要回指该属性,需要将其放在定义该属性的对象的上下文中,即

    let dueDays = this.daysLeft(billings);
    

    必须使用this,例如,如果要调用类函数中的函数,则必须使用
    this
    ,以使类知道引用。因此,您的代码应该如下所示:

    
      amountByMonth(billings){
         let dueDays = this.daysLeft(billings);
    
         // Rest of your code
      }
    

    您能给我们提供一些您尝试过的代码示例吗?如果不在构造函数中设置
    total
    ,几乎不可能对您有所帮助,但您没有将其定义为parameter@ControlAltDel阅读我的代码,当我得到id的值时,它的定义是billing.total=total。这很好。我遇到的问题是,当试图在另一个函数中调用一个函数时。确切的错误是控制台中的“daysLeft未在Bills.amountByMonth中定义”。