Tham khảo tài liệu 'phát triển javascript - part 41', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Event Emitters 373 Listing Expecting chatRoom to be event emitter testCase exports chatRoom should be event emitter function test We can pass this test by popping in as chatRoom s prototype as seen in Listing . Listing chatRoom inheriting from . var EventEmitter require events .EventEmitter . var chatRoom function user message . function id . Note that because V8 fully supports ECMAScript 5 s we could have used property descriptors to add the methods as well as seen in Listing . Listing chatRoom defined with property descriptors var chatRoom addMessage value function user message . getMessagesSince value function id . Download from 374 Server-Side JavaScript with At this point the property descriptors don t provide anything we have a documented need for . the ability to override default property attribute values so we ll avoid the added indentation and stick with the simple assignments in Listing . Next up we make sure that addMessage emits an event. Listing shows the test. Listing Expecting addMessage to emit a message event testCase exports . should emit message event function test var message message function m message m cjno msg .then function m m message To pass this test we need to place a call to emit right before we resolve the promise as seen in Listing . Listing Emitting a message event function user message callback var promise new Promise function . if err . message data data else err true Download from Event Emitters 375 .bind this .