Search code examples
javascriptcallbackpapaparse

Using this in the chunk callback function


I have a class which can parse a file using papa parse, now I would like to pass it a chunk callback function (which is also part of the class).

in the function i would like to use the this which should be pointing to the objects scope (but its pointing to the global scope)

is this possible?


Solution

  • It is possible, you can use bind: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

    Also pay attention to call and apply

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

    And of course ES6 arrow functions, that binds this to the current context

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions