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

173
Vistas
Why does Object.prototype.toString return constructor function name on some objects?

Let's say we do this:

function Dog(name, breed) {
  this.name = name;
  this.breed = breed;

}

var date = new Date();
var dog = new Dog("YOU'RE", "READING");

var zehbi = Object.prototype.toString.call(date);
var zehbi2 = Object.prototype.toString.call(dog);

console.log(zehbi);
console.log(zehbi2);

Why does zehbi return the name of its constructor function "Date" but zehbi2 doesn't return the name of it's constructor function "Dog" but returns "Object" instead?

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

0

Because it is defined that way in the specification:

20.1.3.6 Object.prototype.toString ( )
When the toString toString method is called, the following steps are taken:

  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 "]".

And for Date the point 12. is true because it has an [[DateValue]] internal slot, your Dog does not match any of the 5. to 13. so 14. is used. After that 15. checks for the existence of @@toStringTag which does not return a string for Dog or Date, so the tag becomes the builtinTag given by one of the 5. to 14..

about 4 years ago · Juan Pablo Isaza Denunciar

0

The result of Object.prototype.toString() is determined by two things:

  • The type of built-in object it is executed against. The specification determine what built-ins will be identified as and a Date will say "[object Date]".
  • As of ES6 - the well-known symbol @@toStringTag.

For the normal objects, prior to ES6 the default toString() would only produce "[object Object]" while post ES6 the result is the string "[object @@toStringTag]" by using the value of the symbol. By default the value for the symbol is "Object".

You can override it for a custom class:

function Dog(name, breed) {
  this.name = name;
  this.breed = breed;
}

Dog.prototype[Symbol.toStringTag] = "Dog";

var dog = new Dog("YOU'RE", "READING");

var zehbi2 = Object.prototype.toString.call(dog);

console.log(zehbi2);

Or any object:

var obj = { 
  [Symbol.toStringTag]: "Custom" 
}

console.log(Object.prototype.toString.call(obj));
console.log(obj.toString());

You can even override it for built-in objects:

var obj = new Date()

obj[Symbol.toStringTag] = "Custom";

console.log(Object.prototype.toString.call(obj));

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