Object.prototype

Object.prototype 代表 Object 的原型物件。

Property attributes of Object.prototype
Writable no
Enumerable no
Configurable no

描述

All objects in JavaScript are descended from 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 toString() (en-US) 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.

屬性

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

方法

Object.prototype.__defineGetter__() (en-US) Non-Standard Deprecated
Associates a function with a property that, when accessed, executes that function and returns its return value.
Object.prototype.__defineSetter__() (en-US) Non-Standard Deprecated
Associates a function with a property that, when set, executes that function which modifies the property.
Object.prototype.__lookupGetter__() (en-US) Non-Standard Deprecated
Returns the function associated with the specified property by the __defineGetter__ (en-US) method.
Object.prototype.__lookupSetter__() (en-US) Non-Standard Deprecated
Returns the function associated with the specified property by the __defineSetter__ (en-US) method.
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.
Object.prototype.isPrototypeOf() (en-US)
Returns a boolean indication whether the specified object is in the prototype chain of the object this method is called upon.
Object.prototype.propertyIsEnumerable() (en-US)
Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
Object.prototype.toSource() (en-US) Non-Standard
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.
Object.prototype.toLocaleString() (en-US)
Calls toString() (en-US).
Object.prototype.toString() (en-US)
Returns a string representation of the object.
Object.prototype.unwatch() Non-Standard
Removes a watchpoint from a property of the object.
Object.prototype.valueOf() (en-US)
Returns the primitive value of the specified object.
Object.prototype.watch() Non-Standard
Adds a watchpoint to a property of the object.
Object.prototype.eval() Deprecated
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();

規格

{{Specifications}}

瀏覽器相容性

{{Compat}}

參見