Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3 from Jimgskoop/master
Browse files Browse the repository at this point in the history
Updated README
  • Loading branch information
Jakob Heuser committed Nov 21, 2011
2 parents 2db3b45 + ba85839 commit 49dec12
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion README
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,4 +8,60 @@ Unminified: 9.11k
Minified: 2.06k Minified: 2.06k
Minified+gzip: <1.7k Minified+gzip: <1.7k


What's not to love? What's not to love?




PTClass JS Inheritance Findings

"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."
--Douglas Crockford

// Classes
var A = Class.create( {
id: "A",
initialize: function () {
this.foo = function () {
return "A - PRIVILEGED, from " + this.id;
};
},
foo: function () {
return "A - PROTO, from " + this.id;
}
} );


var B = Class.create( A, {
id: "B",
initialize: function($super){
//$super();
},
foo: function ($super) {
return $super() + " :: B - PROTO, from " + this.id;
}
});



var a = new A();
console.log(a.yell());

var b = new B();
console.log(b.yell());

1. For A.foo and A.prototype.foo, instance a.foo() calls the privileged method A.foo.

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.

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.

4. Next, adding $super() within B's initialize method, restores the inheritance chain as in case #2 above.

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.

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.

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.

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 49dec12

Please sign in to comment.