我有一个包含10个方法的类,在这10个方法中,只有8个应被导出,另外2个用于验证令牌,并且我不希望将其导出,因为用户不需要调用它们(我正在为npmjs写一个包),我的问题是,当类本身使用其他两个方法时,如何仅使用这8种方法导出类?
编辑:添加了一些代码
class StackClass {
constructor(p1,p2) {
this.data = {
p1,p2,accessToken: null,expiresIn: null,tokenType: null
};
}
async getToken() {
return new Promise(async (resolve,reject) => {
try {
let fetchResponse = await fetch(
"url with this.data.p1 and this.data.p2"
);
let fetchData = await fetchResponse.json();
if (fetchData.status != 200) {
reject("ERROR");
} else {
// get the token and save it in this.data
this.data.accessToken = fetchData.access_token;
this.data.expiresIn = fetchData.expires_in;
this.data.tokenType = fetchData.token_type;
resolve(fetchData);
}
} catch (err) {
console.log(err);
}
});
}
async isTokenValid() {
if (new Date() >= this.data.expiresIn) {
// Generate a new Token
try {
await this.getToken();
} catch (err) {
console.log(err);
}
}
// else - Do nothing and use current token saved in this.data.accessToken
}
async getData() {
try {
await this.isTokenValid();
await fetch("url2 with this.data.accessToken");
} catch (err) {
console.log(err);
}
}
}
let user = new StackClass("username","password");
user.isTokenValid(); // SHOULD BE PRIVATE
user.getToken(); // SHOULD BE PRIVATE
user.getData(); // SHOULD BE PUBLIC