We can often discover the pattern like func('function_name', ...args) when using client sdk, and gtag is one of the examples.
gtag('config', 'GA_MEASUREMENT_ID', { 'send_page_view': false });
Why was this method not developed like gtag.config('GA_MEASUREMENT_ID', { 'send_page_view': false }); ?
Is there a word that refers to this pattern?
If there is, why do we use this pattern and what are the advantages and disadvantages?
Thank you for your answer in advance.
The ...args syntax is known as a rest parameter. This allows a function/method to accept any amount of arguments as an array.
Advantages:
{ 'send_page_view': false } in your example.gtag function.Disadvantages:
To reflect on gtag; it requires different parameters depending on the value of the first (command) parameter. The use of a rest parameter here is paramount as it facilitates the ability for gtag to parse and handle different combinations of arguments all in the same function. As I did mention, maintaining arguments via tuples can sometimes get out of hand, but going by the documentation - the number of arguments here is limited to 4 which keeps the cohesion for this API.