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

136
Visualizações
custom function check type of javascipt

I see a function check type in javascript but I don't understand how it works

const typeOf = value => Object.prototype.toString.call(value).slice(8,-1)
typeOf(1) //=> number
typeOf("a") //=> string
typeOf([1]) //=> array
typeOf(()=> {}) //=> function

Please help me explain

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Object.prototype.toString is similar to a big switch statement on the value it's called on. What it does is:

1. If the this value is undefined, return "[object Undefined]".
2. If the this value is null, return "[object Null]".
3. Let O be ! ToObject(this value).
4. Let isArray be ? IsArray(O).
5. If isArray is true, let builtinTag be "Array".
6. Else if O has a [[ParameterMap]] internal slot, let builtinTag be "Arguments".
7. Else if O has a [[Call]] internal method, let builtinTag be "Function".
8. Else if O has an [[ErrorData]] internal slot, let builtinTag be "Error".
9. Else if O has a [[BooleanData]] internal slot, let builtinTag be "Boolean".
10. Else if O has a [[NumberData]] internal slot, let builtinTag be "Number".
11. Else if O has a [[StringData]] internal slot, let builtinTag be "String".
12. Else if O has a [[DateValue]] internal slot, let builtinTag be "Date".
13. Else if O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
14. Else, let builtinTag be "Object".
15. Let tag be ? Get(O, @@toStringTag).
16. If Type(tag) is not String, set tag to builtinTag.
17. Return the string-concatenation of "[object ", tag, and "]".

It essentially iterates through different types of language objects and will create a string corresponding to the one that it matches. For example, if a number is passed, the string generated is "Number".

This string is then concatenated to produce

[object Number]
        ^^^^^^

where the ^^^ section varies depending on what sort of argument was passed.

Doing .slice(8,-1) is like reversing step 17 - it takes the substring starting from index 8 (right after the space) to the next to last character in the string (inclusive) - so the substring that is extracted is "Number" or "Boolean" or something similar.

about 4 years ago · Juan Pablo Isaza 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