Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda