--- title: Object.prototype slug: conflicting/Web/JavaScript/Reference/Global_Objects/Object tags: - JavaScript - 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}}

Object.prototype 代表 {{jsxref("Object")}} 的原型物件。

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

描述

All objects in JavaScript are descended from {{jsxref("Object")}}; all objects inherit methods and properties from Object.prototype, although they may be overridden (except an Object with a null prototype, i.e. Object.create(null)). For example, other constructors' prototypes override the constructor property and provide their own {{jsxref("Object.prototype.toString()", "toString()")}} methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.

屬性

{{jsxref("Object.prototype.constructor")}}
Specifies the function that creates an object's prototype.
{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}
Points to the object which was used as prototype when the object was instantiated.
{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}
Allows a function to be defined that will be executed when an undefined object member is called as a method.
{{jsxref("Object.prototype.__count__")}} {{obsolete_inline}}
Used to return the number of enumerable properties directly on a user-defined object, but has been removed.
{{jsxref("Object.prototype.__parent__")}} {{obsolete_inline}}
Used to point to an object's context, but has been removed.

方法

{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Associates a function with a property that, when accessed, executes that function and returns its return value.
{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Associates a function with a property that, when set, executes that function which modifies the property.
{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Returns the function associated with the specified property by the {{jsxref("Object.defineGetter", "__defineGetter__")}} method.
{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
Returns the function associated with the specified property by the {{jsxref("Object.defineSetter", "__defineSetter__")}} method.
{{jsxref("Object.prototype.hasOwnProperty()")}}
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.
{{jsxref("Object.prototype.isPrototypeOf()")}}
Returns a boolean indication whether the specified object is in the prototype chain of the object this method is called upon.
{{jsxref("Object.prototype.propertyIsEnumerable()")}}
Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}
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.
{{jsxref("Object.prototype.toLocaleString()")}}
Calls {{jsxref("Object.toString", "toString()")}}.
{{jsxref("Object.prototype.toString()")}}
Returns a string representation of the object.
{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}
Removes a watchpoint from a property of the object.
{{jsxref("Object.prototype.valueOf()")}}
Returns the primitive value of the specified object.
{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}
Adds a watchpoint to a property of the object.
{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}
Used to evaluate a string of JavaScript code in the context of the specified object, but has been removed.

範例

因為 JavaScript 並沒有子類別的物件,所以原型是個很有用的解決辦法, 使某些函數作為物件的基本類別物件。例如:

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

規範

Specification Status Comment
{{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')}}  

瀏覽器相容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

參見