javascript - Coffeescript incorrect scope of @ when passing method as callback -
when pass anonymous function fat arrows socket.io callback , call method in same object (as follows) scope of @
correct:
module.exports = class invitecreatesocket extends abstractsocket register: () -> @socket.on 'invite:create', (data, callback) => @create data, callback create: (data = {}, callback = @_noop) -> # returns instantiated invitecreatesocket. bonzer! console.log @
however, if pass in directly, scope of socket, if had run previous code thin arrows:
module.exports = class invitecreatesocket extends abstractsocket register: () -> @socket.on 'invite:create', @create create: (data = {}, callback = @_noop) -> # returns socket. not bonzer. not bonzer @ all. console.log @
so, is there nice clean way of getting object scope without having relay them through fat anonymous function? first method works seems bit clumsy , requires having synchronise parameters in methods. in advance!
why not use fat arrow functions belonging class, binding them correctly context of class (and it's instances) - more how intended work.
module.exports = class invitecreatesocket extends abstractsocket register: () => @socket.on 'invite:create', @create return create: (data = {}, callback = @_noop) => console.log @ return
also, don't forget add empty returns in function not intended return something.
Comments
Post a Comment