Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>...does the Prototype function have any purpose other than to achieve a mechanism of inheritance?</p> </blockquote> <p>I assume you mean the <code>prototype</code> property of <code>Function</code> objects. (There is no "Prototype function" in JavaScript.)</p> <p>It depends on what you mean by "inheritance." The purpose of the <code>prototype</code> property on <code>Function</code> instances is to specify the object that will be assigned as the prototype of objects created using that function as a constructor, e.g.:</p> <pre><code>var f = new Foo(); // Assigns `Foo.prototype` as `f`'s prototype </code></pre> <p>That way, <code>f</code> has access to the properties defined by that prototype. If that's what you mean by inheritance, then yes, that's the only purpose for the <code>prototype</code> property on functions.</p> <p>Note that now we have ECMAScript5, you can assign prototypes without using constructor functions, if you prefer, using the new <code>Object.create</code>:</p> <pre><code>var f = Object.create(somePrototype); // Assigns `somePrototype` as `f`'s prototype </code></pre> <p>Here's a more concrete example of both of those:</p> <p>Using a constructor function:</p> <pre><code>function Foo(name) { this.name = name; } Foo.prototype.hey = function() { console.log("Hi, I'm " + this.name); }; var f = new Foo("Fred"); // Assigns `Foo.prototype` as `f`'s prototype f.hey(); // Shows "Hi, I'm Fred" because `f` gets `hey` from its prototype </code></pre> <p>Using <code>Object.create</code>:</p> <pre><code>var fproto = { hey: function() { console.log("Hi, I'm " + this.name); } }; var f = Object.create(fproto); // Assigns `fproto` as `f`'s prototype f.name = "Fred"; f.hey(); // Shows "Hi, I'm Fred" because `f` gets `hey` from its prototype </code></pre> <p>So if you prefer only to use <code>Object.create</code>, and you're only using ES5-enabled engines, you could never use <code>Function#prototype</code> at all if you like.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload