I have an object called headers. Inside which I want to add certain headers with some random value like:
JavaScript
x
6
1
configs = {
2
header : {
3
'x-some-id': Math.random().toString()
4
}
5
}
6
The config paramter is used to client which is used to send http requests. And the randomid is some id generated by a load balancer. So it will we different for every request. We dont want to create a new client for every client, hence I want to use getter function in header so that everytime a request is made, the header is automatically populated with a new id. How do I implement this using getters. ideally this is what I what to achieve:
JavaScript
1
6
1
configs = {
2
header : {
3
'x-some-id': get() { return Math.random().toString()}
4
}
5
}
6
Advertisement
Answer
The syntax for getters is
JavaScript
1
6
1
configs = {
2
header: {
3
get 'x-some-id'() { return Math.random().toString(); },
4
},
5
};
6