Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updated README with some findings

  • Loading branch information...
commit ba85839f2ba99cd7b8579f97027ed5e07dfa3fa1 1 parent 2db3b45
Jim G Jimgskoop authored

Showing 1 changed file with 57 additions and 1 deletion. Show diff stats Hide diff stats

  1. +57 1 README
58 README
@@ -8,4 +8,60 @@ Unminified: 9.11k
8 8 Minified: 2.06k
9 9 Minified+gzip: <1.7k
10 10
11   -What's not to love?
  11 +What's not to love?
  12 +
  13 +
  14 +
  15 +
  16 +PTClass JS Inheritance Findings
  17 +
  18 +"When a member is sought and it isn't found in the object itself, then it is taken from the object's constructor's prototype member."
  19 +--Douglas Crockford
  20 +
  21 + // Classes
  22 + var A = Class.create( {
  23 + id: "A",
  24 + initialize: function () {
  25 + this.foo = function () {
  26 + return "A - PRIVILEGED, from " + this.id;
  27 + };
  28 + },
  29 + foo: function () {
  30 + return "A - PROTO, from " + this.id;
  31 + }
  32 + } );
  33 +
  34 +
  35 + var B = Class.create( A, {
  36 + id: "B",
  37 + initialize: function($super){
  38 + //$super();
  39 + },
  40 + foo: function ($super) {
  41 + return $super() + " :: B - PROTO, from " + this.id;
  42 + }
  43 + });
  44 +
  45 +
  46 +
  47 + var a = new A();
  48 + console.log(a.yell());
  49 +
  50 + var b = new B();
  51 + console.log(b.yell());
  52 +
  53 +1. For A.foo and A.prototype.foo, instance a.foo() calls the privileged method A.foo.
  54 +
  55 +2. If B inherits from A and we create method B.prototype.foo, calling instance b.foo() calls _the privileged method A.foo_. Since B inherits from A which has the privileged method foo, that gets called before the prototype is searched.
  56 +
  57 +3. Now if we create an empty initialize method on B, this seems to break the privileged inheritance chain, since calling B.foo() really calls B.prototype.foo. Furthermore, $super within B.prototype.foo references A.prototype.foo(), not A's privileged method. $super seems to always reference the prototype method.
  58 +
  59 +4. Next, adding $super() within B's initialize method, restores the inheritance chain as in case #2 above.
  60 +
  61 +5. When called from b.foo(), The this keyword points to the instance b even from within the privileged method at A.foo. "this" always refers to the instance.
  62 +
  63 +6. Adding a privileged method onto B overrides the same method on A – B.foo trumps A.foo – but using $super to call the parent method won't work.
  64 +
  65 +7. Calling $super() from within B.initialize after declaring B.foo there will override B.foo with A.foo. Conversely, declaring B.foo after calling $super() will override A.foo.
  66 +
  67 +8. Privileged methods may only consist of private attribute getters, which can then be used by public methods, which in turn can easily be extended.

0 comments on commit ba85839

Please sign in to comment.
Something went wrong with that request. Please try again.