--- title: Object.prototype slug: conflicting/Web/JavaScript/Reference/Global_Objects/Object translation_of: Web/JavaScript/Reference/Global_Objects/Object translation_of_original: Web/JavaScript/Reference/Global_Objects/Object/prototype original_slug: Web/JavaScript/Reference/Global_Objects/Object/prototype ---
{{JSRef}}

La propietat Object.prototype representa el prototipus per a {{jsxref("Object")}}.

{{js_property_attributes(0, 0, 0)}}

Descripció

A JavaScript, tots els objectes hereten de {{jsxref("Object")}}; tots els objectes hereten els mètodes i propietats de Object.prototype, tot i que es poden sobreescriure (excepte un Object amb prototipus null, és a dir, Object.create(null)). Per exemple, altres prototipus de constructors sobreescriuen la propietat constructor i ofereixen els seus propis mètodes {{jsxref("Object.prototype.toString()", "toString()")}}. Els canvis al prototipus Object es propaguen a tots els objectes a no ser que les propietats i mètodes que reben aquests canvis hagin sigut sobreescrites més avall a la cadena de prototipus.

Propietats

{{jsxref("Object.prototype.constructor")}}
Especifica la funció que ha creat el prototipus de l'objecte.
{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}
Referencia l'objecte utilitzat com a prototipus quan aquest objecte va ser instanciat.
{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}
Permet definir una funció que serà executada quan es cridi com mètode un membre no definit.
{{jsxref("Object.prototype.__count__")}} {{obsolete_inline}}
Retornava el nombre de propietats enumerables que hi hagués a un objecte definit per l'usuari. S'ha eliminat.
{{jsxref("Object.prototype.__parent__")}} {{obsolete_inline}}
Referenciava el context d'un objecte. S'ha eliminat.

Mètodes

{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Associa una funció a una propietat que, quan s'accedeix, executa aquesta funció i retorna el seu valor.
{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Associa una funció a una propietat que, quan s'assigna, executa aquesta funció que modifica la propietat.
{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Retorna una funció associada a la propietat especificada pel mètode {{jsxref("Object.defineGetter", "__defineGetter__")}}.
{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Retorna a funció associada a la propietat especificada pel mètode {{jsxref("Object.defineSetter", "__defineSetter__")}}.
{{jsxref("Object.prototype.hasOwnProperty()")}}
Retorna un booleà que indica si l'objecte conté la propietat especificada com una propietat pròpia d'ell mateix en comptes d'heretar-la a través de la cadena de prototipus.
{{jsxref("Object.prototype.isPrototypeOf()")}}
Retorna un booleà que indica si l'objecte espeicfifcat pertany a la cadena de prototipus de l'objecte sobre el que es crida aquest mètode.
{{jsxref("Object.prototype.propertyIsEnumerable()")}}
Retorna un booleà que indica si està activat l'atribut intern DontEnum de l'ECMAScript.
{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}
Retorna un string que conté cofi font que defineix un literal d'objecte que representa l'objecte sobre el que s'executa aquest mètode; aquest valor pot utilitzar-se per a crear un nou objecte.
{{jsxref("Object.prototype.toLocaleString()")}}
Crida el mètode {{jsxref("Object.toString", "toString()")}}.
{{jsxref("Object.prototype.toString()")}}
Retorna una representació d'aquest objecte en forma de string.
{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}
Esborra un watchpoint d'una propietat de l'objecte.
{{jsxref("Object.prototype.valueOf()")}}
Retorna el valor primitiu de l'objecte especificat.
{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}
Afegeix un watchpoint a una propietat de l'objecte.
{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}
Evaluava un string de codi font JavaScript dins el context de l'objecte especificat. S'ha eliminat.

Exemples

Degut a que JavaScript no res semblant a subclasses d'objectes, la propietat prototype és una bona forma d'utilitzar algunes funcions que fan d'objectes com a "classe base". Per exemple:

var Person = function() {
  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);
  this.name = name;
  this.title = title;
};

Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;

Employee.prototype.greet = function() {
  if (this.canTalk) {
    console.log('Hi, I am ' + this.name + ', the ' + this.title);
  }
};

var Customer = function(name) {
  Person.call(this);
  this.name = name;
};

Customer.prototype = Object.create(Person.prototype);
Customer.prototype.constructor = Customer;

var Mime = function(name) {
  Person.call(this);
  this.name = name;
  this.canTalk = false;
};

Mime.prototype = Object.create(Person.prototype);
Mime.prototype.constructor = Mime;

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();

Especificacions

Especificació Estat Comentaris
{{SpecName('ES1')}} {{Spec2('ES1')}} Definició inicial. Implementat a 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')}}  

Compatibilitat amb navegadors

{{CompatibilityTable}}
Característica Chrome Firefox (Gecko) Internet Explorer Opera Safari
Suport bàsic {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Característica Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Suport bàsic {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

 Vegeu també