May 09, 2021 CoffeeScript
You want to bind a callback to an object.
$ ->
class Basket
constructor: () ->
@products = []
$('.product').click (event) =>
@add $(event.currentTarget).attr 'id'
add: (product) ->
@products.push product
console.log @products
new Basket()
By replacing the normal arrow with an equal arrow ( - ) , the function is automatically bound to the object and can access the variable .