课程: JavaScript: Five Advanced Challenges and Concepts

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Managing observers

Managing observers

- [Instructor] Once you have a basic observer pattern up and running, you probably want to extend it with some additional features. Most obviously, the ability to remove an observer to unsubscribe from whatever the observer is observing. Now in the existing code, you'll remember the customers, the observers, are registered in the observer's array on the current product instance. But, in the register observer method, we're not adding the customer. Instead, we're binding the message method within the customer instance to the customer, and then placing that in the array. That means we can't simply remove that entry from the array because the message method will still be bound. Instead, we have to rethink what is happening inside this register observer method. First, let's go down to customer and bind the message method to the current customer. Looks like this, this, message equals this message, bind, this. Now if we…

内容