1- <!doctype html>
1+ <!doctype html>
22< html lang ="en ">
33< head >
44< title > JavaScript Patterns</ title >
55< meta charset ="utf-8 ">
66</ head >
77< body >
88< script >
9- var tree = { } ;
10- tree . decorate = function ( ) {
11- console . log ( 'Make sure the tree won\'t fall' ) ;
12- } ;
9+ /* Title: Decorator
10+ Description: dynamically adds/overrides behaviour in an existing method of an object
11+ */
12+
13+ var tree = { } ;
14+ tree . decorate = function ( ) {
15+ console . log ( 'Make sure the tree won\'t fall' ) ;
16+ } ;
1317
14- tree . getDecorator = function ( deco ) {
15- tree [ deco ] . prototype = this ;
16- return new tree [ deco ] ;
17- } ;
18+ tree . getDecorator = function ( deco ) {
19+ tree [ deco ] . prototype = this ;
20+ return new tree [ deco ] ;
21+ } ;
1822
19- tree . RedBalls = function ( ) {
20- this . decorate = function ( ) {
21- this . RedBalls . prototype . decorate ( ) ;
22- console . log ( 'Put on some red balls' ) ;
23- }
24- } ;
23+ tree . RedBalls = function ( ) {
24+ this . decorate = function ( ) {
25+ this . RedBalls . prototype . decorate ( ) ;
26+ console . log ( 'Put on some red balls' ) ;
27+ }
28+ } ;
2529
26- tree . BlueBalls = function ( ) {
27- this . decorate = function ( ) {
28- this . BlueBalls . prototype . decorate ( ) ;
29- console . log ( 'Add blue balls' ) ;
30- }
31- } ;
30+ tree . BlueBalls = function ( ) {
31+ this . decorate = function ( ) {
32+ this . BlueBalls . prototype . decorate ( ) ;
33+ console . log ( 'Add blue balls' ) ;
34+ }
35+ } ;
3236
33- tree . Angel = function ( ) {
34- this . decorate = function ( ) {
35- this . Angel . prototype . decorate ( ) ;
36- console . log ( 'An angel on the top' ) ;
37- }
38- } ;
37+ tree . Angel = function ( ) {
38+ this . decorate = function ( ) {
39+ this . Angel . prototype . decorate ( ) ;
40+ console . log ( 'An angel on the top' ) ;
41+ }
42+ } ;
3943
40- tree = tree . getDecorator ( 'BlueBalls' ) ;
41- tree = tree . getDecorator ( 'Angel' ) ;
42- tree = tree . getDecorator ( 'RedBalls' ) ;
44+ tree = tree . getDecorator ( 'BlueBalls' ) ;
45+ tree = tree . getDecorator ( 'Angel' ) ;
46+ tree = tree . getDecorator ( 'RedBalls' ) ;
4347
44- tree . decorate ( ) ;
48+ tree . decorate ( ) ;
4549
4650// reference
47- // http://www.jspatterns .com/
51+ // http://www.addyosmani .com/resources/essentialjsdesignpatterns/book/#decoratorpatternjavascript
4852// http://shop.oreilly.com/product/9780596806767.do?sortby=publicationDate
49- // http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/
5053</ script >
5154</ body >
5255</ html >
0 commit comments