Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>This lead me to think that I haven't set the Object's prototype, merely created a property called prototype, and set that to a new Sprite.</p> </blockquote> <p>That's right. Also, you are not making use of the function's prototype at all, since you assign each function directly to the instance (<code>this.Draw = function() ...</code>) instead of the prototype.</p> <hr> <p>In JavaScript you have something called <em>constructor functions</em>. Every function called with the <code>new</code> operator is a constructor function.</p> <p>When a function is called that way (<code>new SomeFunc()</code>), a new object is created (lets call it an <em>isntance</em>) which inherits from the object referenced by <code>SomeFunc.prototype</code> and this instance is available inside that function via <code>this</code>.</p> <p>Now, inheritance basically comes down to having a prototype (<code>SomeFunc.prototype</code>) which instead of being an (nearly) empty object (the default), inherits from another function's prototype.</p> <p><strong>Example:</strong></p> <pre><code>function A(name) { // `this` refers to a new instance // lets set some properties: this.name = name; } // all "class" methods should be assigned to the prototype A.prototype.getName = function() { return this.name; }; // lets create a child "class" function B(name, place) { // we have to call the parents constructor with the current instance // and the arguments we want to pass on. // this is like `super(name)` in Java or `A.__init__(self, name)` in Python // (or `super(B, self).__init__(name)` in Python) A.call(this, name); this.place = place; } // here we connect A's prototype with B's prototype in a way that they // stay independent inherits(B, A); // B's "class" methods B.prototype.getPlace = function() { return this.place; }; // now we can do var b = new B('SomeName', 'SomePlace'); alert(b.getName()); alert(b.getPlace()); </code></pre> <p>This is how <code>inherits</code> could look like (this implementation is used by the Google Closure library):</p> <pre><code>function inherits(Child, Parent) { var Tmp = function() {}; Tmp.prototype = Parent.prototype; Child.prototype = new Tmp(); Child.prototype.constructor = Child; } </code></pre> <p>You see that <code>Child.prototype</code> refers to an instance of <code>Tmp</code>. But <code>Tmp</code>'s prototype is the same as <code>Parent</code>'s prototype. That means, the instance returned by <code>new Tmp()</code> inherits from <code>Parent</code>'s prototype, and since this instance is the prototype of <code>Child</code>, all instances of <code>Child</code> will inherit from <code>Parent.prototype</code> as well.</p> <p>With ECMAScript 5, this can be simplified to </p> <pre><code>Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; </code></pre> <p>but it's essentially the same. It creates a new object which inherits from <code>Parent.prototype</code>.</p> <hr> <p><strong>Further reading:</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/7944880/js-confusion-about-inheritance">JS: Confusion about inheritance</a></li> <li><a href="https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_constructor_prototype" rel="nofollow noreferrer">MDN - Inheritance and the prototype chain</a></li> <li><a href="https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited" rel="nofollow noreferrer">MDN - Inheritance revisited</a></li> <li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/new" rel="nofollow noreferrer">MDN - <code>new</code></a></li> <li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/this" rel="nofollow noreferrer">MDN - <code>this</code></a></li> <li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call" rel="nofollow noreferrer">MDN - <code>.call()</code></a></li> </ul>
    singulars
    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.
 

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