- Creating configuration objects with dynamic properties.
function createConfig(url, method, headers) {
return {
url,
method,
headers,
getConfig() {
return {
url: this.url,
method: this.method,
headers: this.headers
};
}
};
}
const config1 = createConfig('<https://api.example.com>', 'GET', { 'Content-Type': 'application/json' });
const config2 = createConfig('<https://api.example.com>', 'POST', { 'Content-Type': 'application/x-www-form-urlencoded' });
console.log(config1.getConfig());
// Output: { url: '<https://api.example.com>', method: 'GET', headers: { 'Content-Type': 'application/json' } }
console.log(config2.getConfig());
// Output: { url: '<https://api.example.com>', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }