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

Issue with observer pattern chapter #75

Closed addyosmani closed 11 years ago

addyosmani commented 11 years ago

From the site:

Addy, First, thank you for writing Learning Javascript Design Patterns, I reference it all the time, and would not understand javascript nearly as well without your publication!

Going through the Observer Pattern today, I noticed an omission in the RemoveAt function:

// Currently Printed Online:

ObserverList.prototype.RemoveAt = function( index ){ if( index === 0 ){ this.observerList.shift(); }else if( index === this.observerList.length -1 ){ this.observerList.pop(); } };

// Correction

ObserverList.prototype.RemoveAt = function( index ){ if( index === 0 ){ this.observerList.shift(); }else if( index === this.observerList.length -1 ){ this.observerList.pop(); } else { this.observerList.splice( index, 1 ); };

Thank you again,

Tim