I would like Create an object to store variables which I will use in my web app.
I cannot access the clientId and clientSecret from uriGetToken using this.
Also I can use the function in mApiGetToken in token.
Could you tell me what I’m doing wrong and how to fix it?
$(document).ready(function () {
// General Settings
var mApiSettings = {
clientId: 'aaa',
clientSecret: 'bbb',
token: mApiGetToken(),
uriGetToken: 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + this.clientId + '&client_secret=' + this.clientSecret
}
console.log(mApiSettings.uriGetToken);
// Get Autheticated, it requires getting a Token from HollyByte
function mApiGetToken() {
$.getJSON(mApiSettings.uriGetToken, processData);
function processData(data) {
mApiSettings.token = data.access_token;
}
//return token;
}
// For Testing
console.log(mApiGetToken());
});
The value of
thisis determined for the function in which it appears when that function is called.There is no connection between the object literal in which you are using
thisand the value ofthis.There is also no way to access a property of an object in the middle of the object literal statement that is creating it.
You need to use variables.
Although your examples don’t have any special characters in them, you should make it a habit to escape any data you are inserting into a URI.