mozilla

Revision 361057 of Object.prototype

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

Revision Content

Summary

Represents the Object prototype object.

Description

All objects in JavaScript are descended from Object Object; all objects inherit methods and properties from Object.prototype 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

constructor
Specifies the function that creates an object's prototype.
__count__ {{js_obsolete_inline("1.8.5")}}
Returns the number of enumerable properties directly on a user-defined object.
__parent__ {{js_obsolete_inline("1.8.5")}}
Points to an object's context.
__proto__ {{Non-standard_inline}}
Points to the object which was used as prototype when the object was instantiated.

Methods

__defineGetter__ {{Non-standard_inline}}
Associates a function with a property that, when accessed, executes that function and returns its return value.
__defineSetter__ {{Non-standard_inline}}
Associates a function with a property that, when set, executes that function which modifies the property.
eval {{js_obsolete_inline("1.8.5")}}
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.
__lookupGetter__ {{Non-standard_inline}}
Returns the function associated with the specified property by the __defineGetter__ method.
__lookupSetter__ {{Non-standard_inline}}
Returns the function associated with the specified property by the __defineSetter__ method.
__noSuchMethod__ {{Non-standard_inline}}
Allows a function to be defined that will be executed when an undefined object member is called as a method.
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 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);
        }
    }
}

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

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

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Represents the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object">Object</a> prototype object.</p>
<h2 id="Description" name="Description">Description</h2>
<p>All objects in JavaScript are descended from <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a> Object; all objects inherit methods and properties from <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/prototype" title="JavaScript/Reference/Global_Objects/Object/prototype">Object.prototype</a> <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>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/constructor" title="JavaScript/Reference/Global_Objects/Object/constructor"><code>constructor</code></a></dt>
  <dd>
    Specifies the function that creates an object's prototype.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/count" title="JavaScript/Reference/Global_Objects/Object/count"><code>__count__</code></a> {{js_obsolete_inline("1.8.5")}}</dt>
  <dd>
    Returns the number of enumerable properties directly on a user-defined object.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/Parent" title="JavaScript/Reference/Global_Objects/Object/parent"><code>__parent__</code></a> {{js_obsolete_inline("1.8.5")}}</dt>
  <dd>
    Points to an object's context.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/proto" title="JavaScript/Reference/Global_Objects/Object/proto"><code>__proto__</code></a> {{Non-standard_inline}}</dt>
  <dd>
    Points to the object which was used as prototype when the object was instantiated.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineGetter" title="JavaScript/Reference/Global_Objects/Object/defineGetter"><code>__defineGetter__</code></a> {{Non-standard_inline}}</dt>
  <dd>
    Associates a function with a property that, when accessed, executes that function and returns its return value.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineSetter" title="JavaScript/Reference/Global_Objects/Object/defineSetter"><code>__defineSetter__</code></a> {{Non-standard_inline}}</dt>
  <dd>
    Associates a function with a property that, when set, executes that function which modifies the property.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/eval" title="JavaScript/Reference/Global_Objects/Object/eval"><code>eval</code></a> {{js_obsolete_inline("1.8.5")}}</dt>
  <dd>
    Evaluates a string of JavaScript code in the context of the specified object.</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/lookupGetter" title="JavaScript/Reference/Global_Objects/Object/lookupGetter"><code>__lookupGetter__</code></a> {{Non-standard_inline}}</dt>
  <dd>
    Returns the function associated with the specified property by the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineGetter" title="JavaScript/Reference/Global_Objects/Object/defineGetter">__defineGetter__</a> method.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/lookupSetter" title="JavaScript/Reference/Global_Objects/Object/lookupSetter"><code>__lookupSetter__</code></a> {{Non-standard_inline}}</dt>
  <dd>
    Returns the function associated with the specified property by the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineSetter" title="JavaScript/Reference/Global_Objects/Object/defineSetter">__defineSetter__</a> method.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/noSuchMethod" title="JavaScript/Reference/Global_Objects/Object/noSuchMethod"><code>__noSuchMethod__</code></a> {{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>
  <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>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>
<p><!--Old Data--><!-- languages({
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/prototype"
}) --><br />
  <code>var Employee = function(name, title) {<br />
  &nbsp;&nbsp;&nbsp; this.name = name;<br />
  &nbsp;&nbsp;&nbsp; this.title = title;<br />
  &nbsp;&nbsp;&nbsp; this.greet = function() {<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (this.canTalk) {<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log("Hi, I'm "+this.name+", the "+this.title);<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
  &nbsp;&nbsp;&nbsp; }<br />
  }<br />
  <br />
  var Customer = function(name) {<br />
  &nbsp;&nbsp;&nbsp; this.name = name;<br />
  }<br />
  var Mime = function(name) {<br />
  &nbsp;&nbsp;&nbsp; this.name = name;<br />
  &nbsp;&nbsp;&nbsp; this.canTalk = false;<br />
  }<br />
  <br />
  var Person = {<br />
  &nbsp;&nbsp;&nbsp; canTalk : true,<br />
  &nbsp;&nbsp;&nbsp; greet : function() {<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (this.canTalk) {<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log("Hi, I'm "+this.name)<br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
  &nbsp;&nbsp;&nbsp; }<br />
  }<br />
  Customer.prototype = Person;<br />
  Employee.prototype = Person;<br />
  Mime.prototype = Person;<br />
  <br />
  var bob = new Employee('Bob','Builder');<br />
  var joe = new Customer('Joe');<br />
  var rg = new Employee('Red Green','Handyman');<br />
  var mike = new Customer('Mike');<br />
  var mime = new Mime('Mime');<br />
  bob.greet();<br />
  joe.greet();<br />
  rg.greet();<br />
  mike.greet();<br />
  mime.greet();</code></p>
<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>
Revert to this revision