class @ChatApp # public constructor: (config) -> @dispatcher = new WebSocketRails(window.location.host + "/websocket") bindEvents() log: (message) => $('#chat_history').append message #private bindEvents = -> console.log this # указывает на window console.log @dispatcher # undefined console.log @log # undefined @dispatcher.bind 'log', @log # немного исправляет ситуацию, если написать bindEvents = => console.log this # заменяется на ChatApp console.log @dispatcher # ChatApp.dispatcher = undefined console.log @log # ChatApp.log = undefined @dispatcher.bind 'log', @log the code gives an Uncaught TypeError: Cannot read property 'bind' of undefined error Uncaught TypeError: Cannot read property 'bind' of undefined
binddirectly in the constructor - GrundybindEvents = =>- srghmabindEventsis an ordinary local function, not related to the object being constructed. Therefore, without passing by the parameter, or by settingthismanually, you can’t refer to the object being created - Grundy