--- 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 ---
A propriedade Object.prototype
representa o prototype object {{jsxref("Object","Objeto")}} .
Nearly all objects in JavaScript are instances of {{jsxref("Object")}}; a typical object inherits properties (including methods) from Object.prototype
, although these properties may be shadowed (a.k.a. overridden). However, an Object
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")}}).
Changes to the Object
prototype object are seen by all 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.
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.
When a function is called, the arguments to the call are held in the array-like "variable" arguments. 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.
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); } }
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:
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();
Especificação | Estado | Comentário |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.0. |
{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-object.prototype', 'Object.prototype')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Object.prototype")}}