1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
---
title: Object.prototype.constructor
slug: Web/JavaScript/Reference/Global_Objects/Object/constructor
tags:
- JavaScript
- Object
- Property
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Object/constructor
original_slug: Web/JavaScript/Referencia/Objetos_globales/Object/constructor
---
<div>{{JSRef("Objetos_globales", "Object")}}</div>
<h2 id="Summary" name="Summary">Resumen</h2>
<p>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'.</p>
<h2 id="Description" name="Description">Descripción</h2>
<p>Todos los objetos tienen una propiedad <code>constructor.</code> Los objetos creados sin explicitar el uso de una función (como son los objetos y las cadenas literales) tendrán una propiedad de <code>constructor</code> que apunta al tipo de constructor del Objeto Fundamento para ese objeto.</p>
<pre class="brush:js">var o = {};
o.constructor === Object; // true
var a = [];
a.constructor === Array; // true
var n = new Number(3);
n.constructor === Number; // true</pre>
<h2 id="Examples" name="Examples">Ejemplos</h2>
<h3 id="Example:_Displaying_the_constructor_of_an_object" name="Example:_Displaying_the_constructor_of_an_object">Ejemplo: Mostrando el constructor de un objeto.</h3>
<p>El siguiente ejemplo crea un prototipo, <code>Tree</code>, y un objeto de este tipo, <code>theTree</code>. El ejemplo muestra entonces la propiedad <code>constructor</code> para el objeto <code>theTree</code>.</p>
<pre class="brush:js">function Tree (name) {
this.name = name;
}
var theTree = new Tree( "Redwood" );
console.log( "theTree.constructor is " + theTree.constructor );</pre>
<p>Este ejemplo muestra la siguiente salida:</p>
<pre class="brush:js">theTree.constructor is function Tree (name) {
this.name = name;
}</pre>
<h3 id="Example:_Changing_the_constructor_of_an_object" name="Example:_Changing_the_constructor_of_an_object">Ejemplo: Cambiando el constructor de un objeto.</h3>
<p>El siguiente ejemplo demuestra como modificar el valor del constructor de objetos genéricos. Solo <code>true</code>, <code>1</code> y <code>"test"</code> 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.</p>
<pre class="brush:js">function Type () {}
var types = [
new Array(),
[],
new Boolean(),
true, // no cambia
<span style="line-height: normal;"> </span>new Date(),
<span style="line-height: normal;"> </span>new Error(),
<span style="line-height: normal;"> </span>new Function(),
function () {},
<span style="line-height: normal;"> </span>Math,
<span style="line-height: normal;"> </span>new Number(),
1, // no cambia
<span style="line-height: normal;"> </span>new Object(),
{},
<span style="line-height: normal;"> </span>new RegExp(),
/(?:)/,
<span style="line-height: normal;"> </span>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" ) );</pre>
<h2 id="Especificaciones">Especificaciones</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificaciones</th>
<th scope="col">Estatus</th>
<th scope="col">Comentario</th>
</tr>
<tr>
<td>ECMAScript 1ra. Edición. Implementado en JavaScript 1.1</td>
<td>Estandar.</td>
<td>Definición inicial.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.4.1', 'Objectprototype.constructor')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.prototype.constructor', 'Object.prototype.constructor')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilidad_con_Navegadores">Compatibilidad con Navegadores</h2>
{{Compat("javascript.builtins.Object.constructor")}}
|