Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

386
Vistas
Why use pattern like func('function_name', ...args) in client sdk?

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The ...args syntax is known as a rest parameter. This allows a function/method to accept any amount of arguments as an array.

Advantages:

  • From an API perspective, the logic inside the function should be able to handle any amount of arguments that will be passed in, which relieves the users of having to worry about the required argument number.
  • Rest parameters can often act as sub-parameters for capturing extra (optional) data relating to function execution; such as { 'send_page_view': false } in your example.
  • Because the parameters are arbitrary, a single function can exhibit functionality that parses parameters differently depending on how many were passed and what their values were in run-time. This behaviour is especially prominent in the gtag function.

Disadvantages:

  • Rest parameters can sometimes lead to bad API design if not used correctly. E.g. expecting the user to pass in the correct amount of arguments in the correct order. This will first of all not play nicely with intellisense as it will not suggest where exactly should each argument be placed (because they will all be processed as a tuple) and the function designer will also need to be very careful in making sure that each argument is parsed in the correct order. All of which can be avoided by using normal arguments instead.
  • Depending on how the function is used, it can arguably be more comprehensive to require an optional array instead of a rest parameter.

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda