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/constructor/index.html | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/object/constructor/index.html (limited to 'files/es/web/javascript/reference/global_objects/object/constructor') diff --git a/files/es/web/javascript/reference/global_objects/object/constructor/index.html b/files/es/web/javascript/reference/global_objects/object/constructor/index.html new file mode 100644 index 0000000000..3871c41fe3 --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/object/constructor/index.html @@ -0,0 +1,161 @@ +--- +title: Object.prototype.constructor +slug: Web/JavaScript/Referencia/Objetos_globales/Object/constructor +tags: + - JavaScript + - Object + - Property + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Object/constructor +--- +
{{JSRef("Objetos_globales", "Object")}}
+ +

Resumen

+ +

Retorna una referencia a la función del {{jsxref("Object")}} que creó el objeto de la instancia. Note que el valor de esta propiedad es una referencia a la función misma, no a un string conteniendo el nombre de la función. El valor es sólo de lectura para valores primitivos tales como 1, true y 'test'.

+ +

Descripción

+ +

Todos los objetos tienen una propiedad constructor. Los objetos creados sin explicitar el uso de una función (como son los objetos y las cadenas literales) tendrán una propiedad de constructor que apunta al tipo de constructor del Objeto Fundamento para ese objeto.

+ +
var o = {};
+o.constructor === Object; // true
+
+var a = [];
+a.constructor === Array; // true
+
+var n = new Number(3);
+n.constructor === Number; // true
+ +

Ejemplos

+ +

Ejemplo: Mostrando el constructor de un objeto.

+ +

El siguiente ejemplo crea un prototipo, Tree, y un objeto de este tipo, theTree. El ejemplo muestra entonces la propiedad constructor para el objeto theTree.

+ +
function Tree (name) {
+   this.name = name;
+}
+
+var theTree = new Tree( "Redwood" );
+console.log( "theTree.constructor is " + theTree.constructor );
+ +

Este ejemplo muestra la siguiente salida:

+ +
theTree.constructor is function Tree (name) {
+    this.name = name;
+}
+ +

Ejemplo: Cambiando el constructor de un objeto.

+ +

El siguiente ejemplo demuestra como modificar el valor del constructor de objetos genéricos. Solo true, 1 y "test" no serán afectados ya que ellos tienen constructores nativos de solo lectura. Este ejemplo demuestra que no siempre es seguro confiar en la propiedad constructor de un objeto.

+ +
function Type () {}
+
+var types = [
+    new Array(),
+    [],
+    new Boolean(),
+    true,             // no cambia
+    new Date(),
+    new Error(),
+    new Function(),
+    function () {},
+    Math,
+    new Number(),
+    1,                // no cambia
+    new Object(),
+    {},
+    new RegExp(),
+    /(?:)/,
+    new String(),
+    "test"            // no cambia
+];
+
+for( var i = 0; i < types.length; i++ ) {
+    types[i].constructor = Type;
+    types[i] = [ types[i].constructor, types[i] instanceof Type, types[i].toString() ];
+}
+
+console.log( types.join( "\n" ) );
+ +

Especificaciones

+ + + + + + + + + + + + + + + + + + + + + + + + +
EspecificacionesEstatusComentario
ECMAScript 1ra. Edición. Implementado en JavaScript 1.1Estandar.Definición inicial.
{{SpecName('ES5.1', '#sec-15.2.4.1', 'Objectprototype.constructor')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-object.prototype.constructor', 'Object.prototype.constructor')}}{{Spec2('ES6')}} 
+ +

Compatibilidad con Navegadores

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
CaracterísticaChromeFirefox (Gecko)Internet ExplorerOperaSafari
Soporte básico{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
CaracterísticaAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Soporte básico{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
+
+ +

 

-- cgit v1.2.3-54-g00ecf