Skip to content

Commit

Permalink
Work in #4618 | Add: tostringtag section (#6999)
Browse files Browse the repository at this point in the history
* Work in #4618 | Add: tostringtag section

* Update files/es/web/javascript/reference/global_objects/symbol/tostringtag/index.md
  • Loading branch information
Jalkhov authored Jul 20, 2022
1 parent 408ce25 commit 58c66e1
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
title: Symbol.toStringTag
slug: Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
original_slug: Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag
browser-compat: javascript.builtins.Symbol.toStringTag
---
{{JSRef}}

El símbolo conocido como **`Symbol.toStringTag`** es una propiedad con valor de cadena que se utiliza en la creación de la descripción de cadena por defecto de un objeto. Se accede a ella internamente mediante el método {{jsxref("Object.prototype.toString()")}}.

{{EmbedInteractiveExample("pages/js/symbol-tostringtag.html")}}{{js_property_attributes(0,0,0)}}

## Ejemplos

### Etiquetas por defecto

```js
Object.prototype.toString.call('foo'); // "[object String]"
Object.prototype.toString.call([1, 2]); // "[object Array]"
Object.prototype.toString.call(3); // "[object Number]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(null); // "[object Null]"
// ... and more
```

### Símbolos toStringTag integrados

```js
Object.prototype.toString.call(new Map()); // "[object Map]"
Object.prototype.toString.call(function* () {}); // "[object GeneratorFunction]"
Object.prototype.toString.call(Promise.resolve()); // "[object Promise]"
// ... and more
```

### Clases personalizadas por defecto en la etiqueta objeto

Al crear su propia clase, JavaScript utiliza por defecto la etiqueta "Object":

```js
class ValidatorClass {}

Object.prototype.toString.call(new ValidatorClass()); // "[object Object]"
```

### Etiqueta personalizada con toStringTag

Ahora, con la ayuda de `toStringTag`, puede establecer su propia etiqueta personalizada:

```js
class ValidatorClass {
get [Symbol.toStringTag]() {
return 'Validator';
}
}

Object.prototype.toString.call(new ValidatorClass()); // "[object Validator]"
```

### toStringTag disponible en todos los objetos del prototipo DOM

Debido a un [cambio en las especificaciones de WebIDL](https://github.com/whatwg/webidl/pull/357) a mediados de 2020, los navegadores están añadiendo una propiedad `Symbol.toStringTag` a todos los objetos prototipo del DOM. Por ejemplo, para acceder a la propiedad `Symbol.toStringTag` de {{domxref("HTMLButtonElement")}}:

```js
let test = document.createElement('button');
test.toString(); // Devuelve [object HTMLButtonElement]
test[Symbol.toStringTag]; // Devuelve HTMLButtonElement
```

## Especificaciones

{{Specifications}}

## Compatibilidad con navegadores

{{Compat}}

## Véase también

- [Polyfill de `Symbol.toStringTag` en `core-js`](https://github.com/zloirock/core-js#ecmascript-symbol)
- {{jsxref("Object.prototype.toString()")}}

0 comments on commit 58c66e1

Please sign in to comment.