-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
files/es/web/javascript/reference/global_objects/symbol/tostringtag/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()")}} | ||
|