addyosmani / essential-js-design-patterns

Repo for my 'Learning JavaScript Design Patterns' book
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
4.83k stars 792 forks source link

Decorator Pattern, this.superclass.constructor() and extend() #103

Closed etlolap closed 10 years ago

etlolap commented 11 years ago

Addy,

These codes were extracted from your example for abstract decorator. I got confused by this.superclass.constructor() and extend().

I can understand this refers to myMacbookPro, an instance of CaseDecorator here. But what does this.superclass refer? Why not just this.constructor(macbook)?

CaseDecorator's superclass is MacbookDecorator? Where did the inheritance happen? Are they part of native Javascript or an 3rd party library?

Thanks

var CaseDecorator = function( macbook ){

// call the superclass's constructor next this.superclass.constructor( macbook );

};

// Let's now extend the superclass extend( CaseDecorator, MacbookDecorator ); .... // Decorate the macbook myMacbookPro = new CaseDecorator( myMacbookPro );

addyosmani commented 10 years ago

I noticed this was also asked over at http://stackoverflow.com/questions/19416520/javascript-this-superclass-constructor-and-extend :) The extend() found here is not part of jQuery nor built into JavaScript itself but was meant to symbolically represent object extension. superclass is a property of this as mentioned, but also needs to be made more clear. That was a failure on my part. Let's fix it!

Going to rewrite this sample to be more clear.

addyosmani commented 10 years ago

Clarifications and a running example added. Please let me know if these aren't clear!