如何从方法 JS 访问 class 变量
How can I access class variable from method JS
我是 javascript
的新手,我正在尝试使用某种方法创建一个简单的 class,但我遇到了问题,我无法弄清楚我做错了什么.
var SpotifyWebApi = require('spotify-web-api-node');
const my_client_id = "xxx";
const my_secret = "xxx";
class Spotify {
constructor() {
this.spotifyApi = new SpotifyWebApi({
redirectUri: 'http://localhost:8081/spotifyCallback',
clientId: my_client_id,
clientSecret: my_secret
});
}
connect() {
console.log(this.spotifyApi.redirectUri);
return spotifyApi.createAuthorizeURL('teststate', ['user-read-private', 'user-read-email']);
};
}
在这里,当我尝试登录控制台 spotifyApi.redirectUri 时,我得到了未定义的信息(尝试使用和不使用 this 关键字)。
这是因为您的库 (https://github.com/thelinmichael/spotify-web-api-node) 在实例化时使用 redirectUri
作为选项,但没有将其作为属性公开。
如果您仍然需要它,请将 redirectUri
放入 class 属性中,如下所示:
const SpotifyWebApi = require('spotify-web-api-node');
class Spotify {
constructor(my_client_id, my_secret) {
this.redirectUri = 'http://localhost:8081/spotifyCallback';
this.spotifyApi = new SpotifyWebApi({
redirectUri: this.redirectUri,
clientId: my_client_id,
clientSecret: my_secret
});
}
connect() {
console.log(this.redirectUri);
return this.spotifyApi.createAuthorizeURL('teststate', ['user-read-private', 'user-read-email']);
};
}
const my_client_id = "xxx";
const my_secret = "xxx";
// Now you can instantiate your class with this :
const spotify = new Spotify(my_client_id, my_secret);
const yourToken = spotify.connect();
我用一些好的做法编辑了我的答案(添加构造函数参数,使用 this
,...)
您访问对象的方式不正确。看下面的代码和输出。
const SpotifyWebApi = require('spotify-web-api-node');
const my_client_id = "xxx";
const my_secret = 'xxx';
const redirectUri='http://localhost:8081/spotifyCallback';
class Spotify {
constructor(my_client_id, my_secret, redirectUri) {
this.spotifyApi = new SpotifyWebApi({
clientId: my_client_id,
clientSecret: my_secret,
redirectUri: redirectUri
});
}
connect() {
console.log(JSON.stringify(spotify.spotifyApi._credentials,null,4));
console.log(this.spotifyApi._credentials.redirectUri);
return this.spotifyApi.createAuthorizeURL(['user-read-private', 'user-read-email'],'teststate');
};
}
//Instantiate
const spotify = new Spotify(my_client_id, my_secret ,redirectUri);
const connectObject = spotify.connect();
输出:
{
"clientId": "xxx",
"clientSecret": "xxx",
"redirectUri": "http://localhost:8081/spotifyCallback"
}
http://localhost:8081/spotifyCallback
此外,您还没有为 createAuthorizeURL
传递正确的参数。看看 signautre abouve 和 spotify-web-api-node
我是 javascript
的新手,我正在尝试使用某种方法创建一个简单的 class,但我遇到了问题,我无法弄清楚我做错了什么.
var SpotifyWebApi = require('spotify-web-api-node');
const my_client_id = "xxx";
const my_secret = "xxx";
class Spotify {
constructor() {
this.spotifyApi = new SpotifyWebApi({
redirectUri: 'http://localhost:8081/spotifyCallback',
clientId: my_client_id,
clientSecret: my_secret
});
}
connect() {
console.log(this.spotifyApi.redirectUri);
return spotifyApi.createAuthorizeURL('teststate', ['user-read-private', 'user-read-email']);
};
}
在这里,当我尝试登录控制台 spotifyApi.redirectUri 时,我得到了未定义的信息(尝试使用和不使用 this 关键字)。
这是因为您的库 (https://github.com/thelinmichael/spotify-web-api-node) 在实例化时使用 redirectUri
作为选项,但没有将其作为属性公开。
如果您仍然需要它,请将 redirectUri
放入 class 属性中,如下所示:
const SpotifyWebApi = require('spotify-web-api-node');
class Spotify {
constructor(my_client_id, my_secret) {
this.redirectUri = 'http://localhost:8081/spotifyCallback';
this.spotifyApi = new SpotifyWebApi({
redirectUri: this.redirectUri,
clientId: my_client_id,
clientSecret: my_secret
});
}
connect() {
console.log(this.redirectUri);
return this.spotifyApi.createAuthorizeURL('teststate', ['user-read-private', 'user-read-email']);
};
}
const my_client_id = "xxx";
const my_secret = "xxx";
// Now you can instantiate your class with this :
const spotify = new Spotify(my_client_id, my_secret);
const yourToken = spotify.connect();
我用一些好的做法编辑了我的答案(添加构造函数参数,使用 this
,...)
您访问对象的方式不正确。看下面的代码和输出。
const SpotifyWebApi = require('spotify-web-api-node');
const my_client_id = "xxx";
const my_secret = 'xxx';
const redirectUri='http://localhost:8081/spotifyCallback';
class Spotify {
constructor(my_client_id, my_secret, redirectUri) {
this.spotifyApi = new SpotifyWebApi({
clientId: my_client_id,
clientSecret: my_secret,
redirectUri: redirectUri
});
}
connect() {
console.log(JSON.stringify(spotify.spotifyApi._credentials,null,4));
console.log(this.spotifyApi._credentials.redirectUri);
return this.spotifyApi.createAuthorizeURL(['user-read-private', 'user-read-email'],'teststate');
};
}
//Instantiate
const spotify = new Spotify(my_client_id, my_secret ,redirectUri);
const connectObject = spotify.connect();
输出:
{
"clientId": "xxx",
"clientSecret": "xxx",
"redirectUri": "http://localhost:8081/spotifyCallback"
}
http://localhost:8081/spotifyCallback
此外,您还没有为 createAuthorizeURL
传递正确的参数。看看 signautre abouve 和 spotify-web-api-node