Class inheritance and composition patterns in YUI 3
Review of some strategies for class inheritance and composition, the relevant APIs and the reasons for choosing one approach over another. Watch the recording on the yuilibrary channel on youtube if you can.
Set) ? this : Y.Object(Set.prototype); // use that instead of this [].push.apply(that._items, arguments); return that; } var set = new Set(‘a’,’b’); Saturday, November 5, 11
Set) ? this : Y.Object(Set.prototype); // use that instead of this [].push.apply(that._items, arguments); return that; } var set = new Set(‘a’,’b’); set instanceof Set; // true Saturday, November 5, 11
Set) ? this : Y.Object(Set.prototype); // use that instead of this [].push.apply(that._items, arguments); return that; } var set = Set(‘a’,’b’); // <-- OOPS! I forgot 'new'! Saturday, November 5, 11
Set) ? this : Y.Object(Set.prototype); // use that instead of this [].push.apply(that._items, arguments); return that; } var set = Set(‘a’,’b’); set instanceof Set; // true Saturday, November 5, 11
Can be used to make factory constructors • Can be used to store original values for revert • Any object can be the prototype • Avoids class explosion Saturday, November 5, 11
account1.holder = 'John Q. Consumer'; var account2 = Y.Object(accountProto); account2.id = 1235; account2.holder = 'Jane B. Investor'; • Useful for some internal logic patterns • Not a good fit for most web app problems • Common use suggests need for a constructor Saturday, November 5, 11
account1.holder = 'John Q. Consumer'; var account2 = Y.Object(accountProto); account2.id = 1235; account2.holder = 'Jane B. Investor'; • Useful for some internal logic patterns • Not a good fit for most web app problems • Common use suggests need for a constructor Saturday, November 5, 11
account1.holder = 'John Q. Consumer'; var account2 = Y.Object(accountProto); account2.id = 1235; account2.holder = 'Jane B. Investor'; CONSTRUC TOR • Useful for some internal logic patterns • Not a good fit for most web app problems • Common use suggests need for a constructor Saturday, November 5, 11
this.holder = holder; } var account1 = new Account(1234, 'John Q. Consumer'); var account2 = new Account(1235, 'Jane B. Invester'); • Useful for some internal logic patterns • Not a good fit for most web app problems • Common use suggests need for a constructor Saturday, November 5, 11
Y.ArrayList); var list = new Y.ModelList({ ... }); list.each(function (item) { ... }); ModelList Prototype Constructor create init ArrayList Prototype each item Constructor each item Prototype create init each item Saturday, November 5, 11
Y.ArrayList); var list = new Y.ModelList({ ... }); list.each(function (item) { ... }); ModelList Prototype Constructor create init ArrayList Prototype each item Constructor each item Prototype Saturday, November 5, 11
list = new Y.ModelList({ ... }); list.each(function (item) { ... }); ModelList Prototype Constructor create init Prototype each item each each Saturday, November 5, 11
list = new Y.ModelList({ ... }); list.each(function (item) { ... }); ModelList Prototype Constructor create init Prototype each item each Saturday, November 5, 11
Y.ArrayList); var list = new Y.ModelList({ ... }); list.each(function (item) { ... }); ModelList Prototype Constructor create init Prototype each item 1. Copy each Saturday, November 5, 11
inheritance • Y.Base-based classes should use class extensions • Beware the diamond problem • Weigh the importance of constructor deferral Saturday, November 5, 11
true); overlay.unplug('dd'); Overlay ATTRS x y Constructor Plugin.Drag ATTRS lock Constructor overlay Attributes x y overlay.dd Attributes lock dd Saturday, November 5, 11
true); overlay.unplug('dd'); Overlay ATTRS x y Constructor Plugin.Drag ATTRS lock Constructor overlay Attributes x y overlay.dd Attributes lock dd Saturday, November 5, 11
via events or AOP MAY MUST ✓ Remove all traces when unplugged SHOULD ✓ Expect an object constructor argument with ‘host’ property Saturday, November 5, 11
via events or AOP MAY MUST ✓ Remove all traces when unplugged MUST NOT ✓ Modify host directly other than add the namespace SHOULD ✓ Expect an object constructor argument with ‘host’ property Saturday, November 5, 11
{ ... }); var series = new Y.LineSeries({ ... }); Prototype ATTRS Constructor ATTRS Cartesian Constructor ATTRS Prototype direction type Lines Constructor ATTRS Prototype type Saturday, November 5, 11
{ ... }); var series = new Y.LineSeries({ ... }); Prototype Constructor ATTRS Cartesian Constructor ATTRS Prototype direction type Lines Constructor ATTRS Prototype type styles Saturday, November 5, 11
{ ... }); var series = new Y.LineSeries({ ... }); Prototype Constructor ATTRS Cartesian Constructor ATTRS Prototype direction type Lines Constructor ATTRS Prototype type direction type type styles Saturday, November 5, 11
{ ... }); var series = new Y.LineSeries({ ... }); Prototype Constructor ATTRS Cartesian Constructor ATTRS Prototype direction type Lines Constructor ATTRS Prototype type direction type styles Saturday, November 5, 11
Y.Base.create('lineSeries', Y.CartesianSeries, [ Y.Lines ], { ... }); var series = new Y.LineSeries({ ... }); Prototype Constructor Cartesian Constructor ATTRS Prototype direction type ATTRS direction type styles Prototype Saturday, November 5, 11
core behavior • Extensions modify the class prototype, plugins are always namespaced • Feature extension constructors are always executed, plugin constructors on plug() • Feature APIs/attributes on the prototype vs class plugins in namespace is a stylistic choice Saturday, November 5, 11