亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Node.js 中運行函數

如何在 Node.js 中運行函數

三國紛爭 2023-03-18 16:24:22
這是我第一次在 Node.js 中創建類,我想知道為什么我不能運行這個函數……有人能給我指出正確的方向嗎?class Customer {  constructor() {    this.shoppingBasket = [];    function startShopping() {      const prompt = require("prompt-sync")();      const itemName = prompt("What item is this?");      const itemPrice = prompt("How much is it?");      const taxExemption = promt("Is this a food, book or medical product?");      console.log(`Your item is ${itemName}`);    }  }}Customer = Customer;Customer.startShopping();
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

按照您在此處制作它的方式,它是一個范圍內的功能......這意味著您無法在您嘗試訪問它的范圍內“看到”它。


您需要使其成為“方法”或將其附加到“此”。


// We normally require/import things at the top, but not nessesarily.

// const PromptSync = require("prompt-sync");


class Customer {

  constructor() {

    this.shoppingBasket = [];



    this.startShoppingPropertyFunction = () => {

      // If imported at top, use this instead:

      // const prompt = new PromptSync();

      const prompt = require("prompt-sync")();

      const itemName = prompt("What item is this?");

      const itemPrice = prompt("How much is it?");

      const taxExemption = prompt("Is this a food, book or medical product?");

      console.log(`Your item is ${itemName}`);

    }

  }


  startShoppingMethod() {

    // If imported at top, use this instead:

    // const prompt = new PromptSync();

    const prompt = require("prompt-sync")();

    const itemName = prompt("What item is this?");

    const itemPrice = prompt("How much is it?");

    const taxExemption = prompt("Is this a food, book or medical product?");

    console.log(`Your item is ${itemName}`);

  }

}


const myCustomer = new Customer();

myCustomer.startShoppingMethod();

myCustomer.startShoppingPropertyFunction();


查看完整回答
反對 回復 2023-03-18
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號