简体   繁体   English

使用 javascript / node.js 将值从一个 function 传递到另一个 class function

[英]Passing value from one function to another class function using javascript / node js

I am trying to pass value from one page function to another Class function but its not working as expectation我正在尝试将值从一页 function 传递到另一页 Class function 但它没有按预期工作

detail.js细节.js

const {UserData} = require("./userData");

let commonfield="Asia Origin";

var userData= new UserData();
userData.start(commonField);

userData.js用户数据.js

class UserData extends Commands{
 async start(field){
  console.log(field);
 }
}

Here You can see I tried to pass the value but its not passing the value from detail.js to userData.js class function在这里你可以看到我试图传递值但它没有将值从 detail.js 传递到 userData.js class function

You are calling the wrong function and remove curly braces from userData :您调用了错误的 function 并从userData中删除花括号:

const UserData = require("./userData");

let commonfield="Asia Origin";

var userData= new UserData();

try {
   const result = await userData.start(commonField); // should be start
} 
catch(e) {
   console.log(e);
}

class UserData extends Commands{
 async start(field){
  console.log(field);
  return field;
 }
}

As, start is an async function to get its value you need to use await.因为, start是一个 async function 来获取您需要使用 await 的值。

You are not exporting the class, you need to do it in order to access it:您没有导出 class,您需要这样做才能访问它:

class UserData extends Commands{
 async start(field){
  console.log(field);
 }
}

export {UserData};

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

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