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

172
Vistas
JavaScript Advanced Syntax - I don't understand

So I recently copied this code from the internet to format strings inline:

module.exports=function(){
    String.prototype.FormatPlus = String.prototype.FormatPlus || function(){
        "use strict";
        let str = this.toString();
        if (arguments.length) {
            let t = typeof arguments[0];
            let key;
            let args = ("string" === t || "number" === t) ? Array.prototype.slice.call(arguments) : arguments[0];
            
            for (key in args) {
                str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]);
            }
            
            return str;
        }
    }
}

I understand most of it, except this part:

let args = ("string" === t || "number" === t) ? Array.prototype.slice.call(arguments) : arguments[0];

Is there any professional JavaScript Node.js programmers that can break-down every bit of this line please?

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

t is the type of the first argument. So the ternary conditional expression tests whether the first argument is a string or number.

If it is, arguments is converted to an array by calling Array.prototype.slice(), and this is assigned to args.

If not, the first argument is assigned to args.

This can be written more clearly, using modern EcmaScript 6 features:

let args;
if (t === 'string' || t == 'number') {
    args = [...arguments];
} else {
    args = arguments[0];
}

The purpose of this is to allow the caller to provide the values to be formatted as a single array or object, or spread as separate arguments.

about 4 years ago · Santiago Gelvez Denunciar

0

Array.prototype.slice() returns a shallow copy of a portion of an array into a new array object.

In the function you provided, t is an alias for typeof arguments[0].

So in your code, when the first element of arguments is a string or a number, you call the Array.prototype.slice() function on every element.

For example, if arguments[0] is the string "hello world", Array.prototype.slice.call("hello world") would return ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'].

about 4 years ago · Santiago Gelvez Denunciar

0

arguments is an array type. if first element of argument array is of type string or number then you are just returning entire argument array itself by calling slice method of Array & assigning it to args.

If first element of argument is not of type string or number then you are returning just first element of argument & assigning it to args.

? is ternary operator in javascript which is similar to if.else block.

about 4 years ago · Santiago Gelvez 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