From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../global_objects/object/tosource/index.html | 126 +++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/object/tosource/index.html (limited to 'files/es/web/javascript/reference/global_objects/object/tosource') diff --git a/files/es/web/javascript/reference/global_objects/object/tosource/index.html b/files/es/web/javascript/reference/global_objects/object/tosource/index.html new file mode 100644 index 0000000000..713a176b0b --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/object/tosource/index.html @@ -0,0 +1,126 @@ +--- +title: Object.prototype.toSource() +slug: Web/JavaScript/Referencia/Objetos_globales/Object/toSource +translation_of: Web/JavaScript/Reference/Global_Objects/Object/toSource +--- +
{{JSRef}} {{non-standard_header}}
+ +

El método toSource() regresa una cadena representando el código fuente del objeto.

+ +

Syntax

+ +
Object.toSource();
+obj.toSource();
+
+ +

Return value

+ +

Una cadena representando el código fuente del objeto.

+ +

Description

+ +

EL método toSource() regresa los siguientes valores:

+ + + +

Puedes llamar el método toSource() durante el depurado para examinar el contenido de un objeto.

+ +

Sobreescribir el método toSource()

+ +

Es seguro para los objetos sobreescribir el método toSource(). Por ejemplo:

+ +
function Person(name) {
+  this.name = name;
+}
+
+Person.prototype.toSource = function Person_toSource() {
+  return 'new Person(' + uneval(this.name) + ')';
+};
+
+console.log(new Person('Joe').toSource()); // ---> nueva Person("Joe")
+
+ +

Métodos de toSource() incorporados

+ +

Cada tipo fundamental de JavaScript tiene su propio método toSource().  Éstos objetos son:

+ + + +

Limitaciones en objetos cíclicos

+ +

EN el caso de los objetos que contienen referencia a ellos mismos, por ejemplo, una lista enlazada cíclicamente o un árbol que puede ser atravesado en ambas formas, toSource() no recreará la referencia a sí mismo, a partir de Firefox 24. Por ejemplo:

+ +
var obj1 = {};
+var obj2 = { a: obj1 };
+obj1.b = obj2;
+
+console.log('Ciclico: ' + (obj1.b.a == obj1));
+
+var objSource = obj1.toSource(); // regresa "({b:{a:{}}})"
+
+obj1 = eval(objSource);
+
+console.log('Ciclico: ' + (obj1.b.a == obj1));
+
+ +

Si una estructura cíclica es usada y se necesita el método toSource(), el objeto debe proveer la sobreescritura de toSource(), ya sea usando una referencia a un constructor o proveyendo una función anónima.

+ +

Ejemplos

+ +

Usando toSource()

+ +

El código siguiente define el objeto tipo Dog y crea a theDog, un objeto tipo Dog:

+ +
function Dog(name, breed, color, sex) {
+  this.name = name;
+  this.breed = breed;
+  this.color = color;
+  this.sex = sex;
+}
+
+theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
+
+ +

Llamando al método toSource() de theDog muestra el código JavaScript que define al objeto:

+ +
theDog.toSource();
+// returns ({name:"Gabby", breed:"Lab", color:"chocolate", sex:"female"})
+
+ +

Especificaciones

+ +

No es parte de ningún estándar. Implementado en JavaScript 1.3.

+ +

Compatibilidad en navegadores

+ +
+ + +

{{Compat("javascript.builtins.Object.toSource")}}

+
+ +

Ver también

+ + -- cgit v1.2.3-54-g00ecf