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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
---
title: Object.prototype
slug: Web/JavaScript/Reference/Global_Objects/Object/prototype
tags:
- JavaScript
- Object
- Objeto
- Propriedade
translation_of: Web/JavaScript/Reference/Global_Objects/Object
translation_of_original: Web/JavaScript/Reference/Global_Objects/Object/prototype
---
<div>{{JSRef}}</div>
<p>A propriedade <code><strong>Object.prototype</strong></code> representa o <em>prototype object</em> {{jsxref("Object","Objeto")}} .</p>
<div>{{js_property_attributes(0, 0, 0)}}</div>
<h2 id="Descrição">Descrição</h2>
<p>Nearly all objects in JavaScript are instances of {{jsxref("Object")}}; a typical object inherits properties (including methods) from <code>Object.prototype</code>, although these properties may be shadowed (a.k.a. overridden). However, an <code>Object</code> may be deliberately created for which this is not true (e.g. by {{jsxref("Object.create", "Object.create(null)")}}), or it may be altered so that this is no longer true (e.g. with {{jsxref("Object.setPrototypeOf")}}).</p>
<p>Changes to the <code>Object</code> prototype object are seen by <strong>all</strong> objects through prototype chaining, unless the properties and methods subject to those changes are overridden further along the prototype chain. This provides a very powerful although potentially dangerous mechanism to override or extend object behavior.</p>
<h2 id="Propriedades">Propriedades</h2>
<dl>
<dt>{{jsxref("Object.prototype.constructor")}}</dt>
<dd>Specifies the function that creates an object's prototype.</dd>
<dt>{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}</dt>
<dd>Points to the object which was used as prototype when the object was instantiated.</dd>
<dt>{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}</dt>
<dd>Allows a function to be defined that will be executed when an undefined object member is called as a method.</dd>
<dt><s class="obsoleteElement">{{jsxref("Object.prototype.count","Object.prototype.__count__")}} {{obsolete_inline}}</s></dt>
<dd><s class="obsoleteElement">Used to return the number of enumerable properties directly on a user-defined object, but has been removed.</s></dd>
<dt><s class="obsoleteElement">{{jsxref("Object.prototype.parent","Object.prototype.__parent__")}} {{obsolete_inline}}</s></dt>
<dd><s class="obsoleteElement">Used to point to an object's context, but has been removed.</s></dd>
</dl>
<h2 id="Métodos">Métodos</h2>
<dl>
<dt>{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt>
<dd>Associates a function with a property that, when accessed, executes that function and returns its return value.</dd>
<dt>{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt>
<dd>Associates a function with a property that, when set, executes that function which modifies the property.</dd>
<dt>{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt>
<dd>Returns the function associated with the specified property by the {{jsxref("Object.prototype.__defineGetter__()", "__defineGetter__()")}} method.</dd>
<dt>{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt>
<dd>Returns the function associated with the specified property by the {{jsxref("Object.prototype.__defineSetter__()", "__defineSetter__()")}} method.</dd>
<dt>{{jsxref("Object.prototype.hasOwnProperty()")}}</dt>
<dd>Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.</dd>
<dt>{{jsxref("Object.prototype.isPrototypeOf()")}}</dt>
<dd>Returns a boolean indicating whether the object this method is called upon is in the prototype chain of the specified object.</dd>
<dt>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</dt>
<dd>Returns a boolean indicating if the internal <a href="/en-US/docs/Web/JavaScript/Data_structures#Properties">ECMAScript [[Enumerable]] attribute</a> is set.</dd>
<dt>{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}</dt>
<dd>Returns string containing the source of an object literal representing the object that this method is called upon; you can use this value to create a new object.</dd>
<dt>{{jsxref("Object.prototype.toLocaleString()")}}</dt>
<dd>Calls {{jsxref("Object.toString", "toString()")}}.</dd>
<dt>{{jsxref("Object.prototype.toString()")}}</dt>
<dd>Returns a string representation of the object.</dd>
<dt>{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}</dt>
<dd>Removes a watchpoint from a property of the object.</dd>
<dt>{{jsxref("Object.prototype.valueOf()")}}</dt>
<dd>Returns the primitive value of the specified object.</dd>
<dt>{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}</dt>
<dd>Adds a watchpoint to a property of the object.</dd>
<dt><s class="obsoleteElement">{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}</s></dt>
<dd><s class="obsoleteElement">Used to evaluate a string of JavaScript code in the context of the specified object, but has been removed.</s></dd>
</dl>
<h2 id="Exemplos">Exemplos</h2>
<p>When altering the behavior of existing Object.prototype methods, consider injecting code by wrapping your extension before or after the existing logic. For example, this (untested) code will pre-conditionally execute custom logic before the built-in logic or someone else's extension is executed.</p>
<p>When a function is called, the arguments to the call are held in the array-like "variable" <a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a>. For example, in the call "myFn(a, b, c)", the arguments within myFn's body will contain 3 array-like elements corresponding to (a, b, c). When modifying prototypes with hooks, simply pass this & the arguments (the call state) to the current behavior by calling apply() on the function. This pattern can be used for any prototype, such as Node.prototype, Function.prototype, etc.</p>
<pre class="brush: js">var current = Object.prototype.valueOf;
// Since my property "-prop-value" is cross-cutting and isn't always
// on the same prototype chain, I want to modify Object.prototype:
Object.prototype.valueOf = function() {
if (this.hasOwnProperty('-prop-value')) {
return this['-prop-value'];
} else {
// It doesn't look like one of my objects, so let's fall back on
// the default behavior by reproducing the current behavior as best we can.
// The apply behaves like "super" in some other languages.
// Even though valueOf() doesn't take arguments, some other hook may.
return current.apply(this, arguments);
}
}
</pre>
<p>Since JavaScript doesn't exactly have sub-class objects, prototype is a useful workaround to make a “base class” object of certain functions that act as objects. For example:</p>
<pre class="brush: js">var Person = function(name) {
this.name = name;
this.canTalk = true;
};
Person.prototype.greet = function() {
if (this.canTalk) {
console.log('Hi, I am ' + this.name);
}
};
var Employee = function(name, title) {
Person.call(this, name);
this.title = title;
};
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.greet = function() {
if (this.canTalk) {
console.log('Hi, I am ' + this.name + ', the ' + this.title);
}
};
var Customer = function(name) {
Person.call(this, name);
};
Customer.prototype = Object.create(Person.prototype);
var Mime = function(name) {
Person.call(this, name);
this.canTalk = false;
};
Mime.prototype = Object.create(Person.prototype);
var bob = new Employee('Bob', 'Builder');
var joe = new Customer('Joe');
var rg = new Employee('Red Green', 'Handyman');
var mike = new Customer('Mike');
var mime = new Mime('Mime');
bob.greet();
// Hi, I am Bob, the Builder
joe.greet();
// Hi, I am Joe
rg.greet();
// Hi, I am Red Green, the Handyman
mike.greet();
// Hi, I am Mike
mime.greet();
</pre>
<h2 id="Especificações">Especificações</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificação</th>
<th scope="col">Estado</th>
<th scope="col">Comentário</th>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.0.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-object.prototype', 'Object.prototype')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilidade_de_navegador">Compatibilidade de navegador</h2>
<div>
<p>{{Compat("javascript.builtins.Object.prototype")}}</p>
</div>
<h2 id="Consultar_também">Consultar também</h2>
<ul>
<li><a href="/pt-PT/docs/Learn/JavaScript/Objects">Introdução ao JavaScript Orientado a Objetos</a></li>
</ul>
|