Revision 492215 of Object.prototype

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/prototype
  • Revision title: Object.prototype
  • Revision id: 492215
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}}

Summary

The Object.prototype property represents the {{jsxref("Global_Objects/Object", "Object")}} prototype object.

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

Description

All objects in JavaScript are descended from {{jsxref("Global_Objects/Object", "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 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.

Properties

{{jsxref("Object.prototype.constructor")}}
Specifies the function that creates an object's prototype.
{{jsxref("Object.count", "Object.prototype.__count__")}} {{obsolete_inline()}}
Returns the number of enumerable properties directly on a user-defined object.
{{jsxref("Object.parent", "Object.prototype.__parent__")}} {{obsolete_inline()}}
Points to an object's context.
{{jsxref("Object.proto", "Object.prototype.__proto__")}} {{Non-standard_inline}}
Points to the object which was used as prototype when the object was instantiated.
{{jsxref("Object.noSuchMethod", "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.

Methods

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

Examples

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() {
    this.canTalk = true;
    this.greet = function() {
        if (this.canTalk) {
            console.log("Hi, I'm " + this.name);
        }
    };
};

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

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

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

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();
joe.greet();
rg.greet();
mike.greet();
mime.greet();

This will output:

Hi, I'm Bob, the Builder
Hi, I'm Joe
Hi, I'm Red Green, the Handyman
Hi, I'm Mike

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}} {{Spec2('ES6')}}  

Browser compatibility

{{ 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() }}

 

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>Object.prototype</strong></code> property represents the {{jsxref("Global_Objects/Object", "Object")}} prototype object.</p>
<p>{{js_property_attributes(0,0,0)}}</p>
<h2 id="Description" name="Description">Description</h2>
<p>All objects in JavaScript are descended from {{jsxref("Global_Objects/Object", "Object")}}; all objects inherit methods and properties from <code>Object.prototype</code>, although they may be overridden (except an <code>Object</code> with a <code>null</code> prototype, i.e. <code>Object.create(null)</code>). For example, other constructors' prototypes override the <code>constructor</code> property and provide their own <code>toString</code> methods. Changes to the <code>Object</code> prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.</p>
<h2 id="Properties">Properties</h2>
<dl>
 <dt>
  {{jsxref("Object.prototype.constructor")}}</dt>
 <dd>
  Specifies the function that creates an object's prototype.</dd>
 <dt>
  {{jsxref("Object.count", "Object.prototype.__count__")}} {{obsolete_inline()}}</dt>
 <dd>
  Returns the number of enumerable properties directly on a user-defined object.</dd>
 <dt>
  {{jsxref("Object.parent", "Object.prototype.__parent__")}} {{obsolete_inline()}}</dt>
 <dd>
  Points to an object's context.</dd>
 <dt>
  {{jsxref("Object.proto", "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.noSuchMethod", "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>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
 <dt>
  {{jsxref("Object.defineGetter", "Object.prototype.__defineGetter__()")}} {{Non-standard_inline}}</dt>
 <dd>
  Associates a function with a property that, when accessed, executes that function and returns its return value.</dd>
 <dt>
  {{jsxref("Object.defineSetter", "Object.prototype.__defineSetter__()")}} {{Non-standard_inline}}</dt>
 <dd>
  Associates a function with a property that, when set, executes that function which modifies the property.</dd>
 <dt>
  {{jsxref("Object.lookupGetter", "Object.prototype.__lookupGetter__()")}} {{Non-standard_inline}}</dt>
 <dd>
  Returns the function associated with the specified property by the {{jsxref("Object.defineGetter", "__defineGetter__")}} method.</dd>
 <dt>
  {{jsxref("Object.lookupSetter", "Object.prototype.__lookupSetter__()")}} {{Non-standard_inline}}</dt>
 <dd>
  Returns the function associated with the specified property by the {{jsxref("Object.defineSetter", "__defineSetter__")}} method.</dd>
 <dt>
  <s class="obsoleteElement">{{jsxref("Object.prototype.eval()")}} {{ obsolete_inline()}}</s></dt>
 <dd>
  <s class="obsoleteElement">Evaluates a string of JavaScript code in the context of the specified object.</s></dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="JavaScript/Reference/Global_Objects/Object/hasOwnProperty"><code>hasOwnProperty</code></a></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>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/isPrototypeOf" title="JavaScript/Reference/Global_Objects/Object/isPrototypeOf"><code>isPrototypeOf</code></a></dt>
 <dd>
  Returns a boolean indication whether the specified object is in the prototype chain of the object this method is called upon.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable"><code>propertyIsEnumerable</code></a></dt>
 <dd>
  Returns a boolean indicating if the internal <a href="/en-US/docs/ECMAScript_DontEnum_attribute" title="ECMAScript_DontEnum_attribute">ECMAScript DontEnum attribute</a> is set.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toSource" title="JavaScript/Reference/Global_Objects/Object/toSource"><code>toSource</code></a> {{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>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toLocaleString" title="JavaScript/Reference/Global_Objects/Object/toLocaleString"><code>toLocaleString</code></a></dt>
 <dd>
  Calls <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" title="JavaScript/Reference/Global_Objects/Object/toString"><code>toString</code></a>.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" title="JavaScript/Reference/Global_Objects/Object/toString"><code>toString</code></a></dt>
 <dd>
  Returns a string representation of the object.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/unwatch" title="JavaScript/Reference/Global_Objects/Object/unwatch"><code>unwatch</code></a> {{Non-standard_inline}}</dt>
 <dd>
  Removes a watchpoint from a property of the object.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/valueOf" title="JavaScript/Reference/Global_Objects/Object/valueOf"><code>valueOf</code></a></dt>
 <dd>
  Returns the primitive value of the specified object.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/watch" title="JavaScript/Reference/Global_Objects/Object/watch"><code>watch</code></a> {{Non-standard_inline}}</dt>
 <dd>
  Adds a watchpoint to a property of the object.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<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() {
    this.canTalk = true;
    this.greet = function() {
        if (this.canTalk) {
            console.log("Hi, I'm " + this.name);
        }
    };
};

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

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

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

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();
joe.greet();
rg.greet();
mike.greet();
mime.greet();
</pre>
<p>This will output:</p>
<p>Hi, I'm Bob, the Builder<br />
 Hi, I'm Joe<br />
 Hi, I'm Red Green, the Handyman<br />
 Hi, I'm Mike</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>&nbsp;</p>
Revert to this revision