일부 JSON으로 응답하는 API에 액세스하려고했습니다. 따라서 API (URL, 토큰, ID 등)에 액세스하는 데 필요한 모든 데이터를 저장하는 'options'객체를 구성하려고합니다. 작동 JavaScript Options-Object를 선언하는 적절한 방법은 무엇입니까?
다음 버전 :
var Options = function (token, id) {
this.token = token;
this.id = id;
this.host = 'http://super-cool-api.com/';
this.path = 'api/fetch/id/';
this.url = '' + this.host + this.path + this.id + '?token=' + this.token
};
var options = new Options('abc', 3);
// options.url = "http://super-cool-api.com/api/fetch/id/3?token=abc"
기본적으로 'options.url는'내가 원하는 전부입니다. 하지만이 같은 '옵션'개체에 대한보다 포괄적 인 형태를 선언 시도했다 :
좋아var options = {
token : 'abc',
id : 3,
host : 'http://super-cool-api.com/',
path : 'api/fetch/id/',
url : '' + this.host + this.path + this.id + '?token=' + this.token
};
// options.url = "undefinedundefinedundefined?token=undefined"
, 나는 어떻게 든 다른 options.url의 값에 액세스 할 필요가 있음을 이해했다. 그러나 어떻게? 심지어 이것이 일반적인 관행입니까?
첫 번째 해결책은 무엇입니까? 이런 식으로하는 것이 좋습니까?
감사합니다, 크리스토프