0

What is the significance / meaning of

  • @ prefix
  • difference between -> and =>

Is there some documentation I can find for it? It's hard to google for these special chars so I'm asking here. [ full gist ]

class RP.Dashboard.Events.Form extends Backbone.View
  el: '.simple_form.new_event, .simple_form.edit_event, .simple_form#new_event, .simple_form#edit_event'
  events:
    'focus #location_autocomplete': 'geolocate'
  address_component_map:
    street_number:
      key: 'short_name'
      form_field: '#event_address'


  initialize: ->
    @render()
    @init_autocomplete()

  render: ->
    @$(".datepicker" ).datepicker(
      showOn: "button"
      buttonImageOnly: true
      changeMonth: true
      changeYear: true
      format: 'yyyy-mm-dd'
      dateFormat: 'yy-mm-dd'
    )
    @$(".timepicker").timepicker()
    @$('.input-timepicker').timepicker({minuteStep: 1,showSeconds: false,showMeridian: true,defaultTime: '8'});


  fill_in_address: =>
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
american-ninja-warrior
  • 7,397
  • 11
  • 46
  • 80

1 Answers1

1

Googling "coffeescript at sign" gives Does the '@' symbol have special meaning in Javascript, Coffeescript or Jquery? as the top result. @ means this in coffeescript:

## coffeescript
self = @ 

## javascript
var self = this

Regarding the difference between -> and =>, googling "cofeescript ->" returns this as the top link:

CoffeeScript, When to use fat arrow (=>) over arrow (->) and vice versa

To copy and paste from there:

The major use-case I've found for the fat-arrow in defining methods is when you want to use a method as a callback and that method references instance fields:

class A
  constructor: (@msg) ->
  thin: -> alert @msg
  fat:  => alert @msg

x = new A("yo")
x.thin() #alerts "yo"
x.fat()  #alerts "yo"

fn = (callback) -> callback()

fn(x.thin) #alerts "undefined"
fn(x.fat)  #alerts "yo"
fn(-> x.thin()) #alerts "yo"

As you see, you may run into problems passing a reference to an instance's method as a callback if you don't use the fat-arrow. This is because the fat-arrow binds the instance of the object to this whereas the thin-arrow doesn't, so thin-arrow methods called as callbacks as above can't access the instance's fields like @msg or call other instance methods. The last line there is a workaround for cases where the thin-arrow has been used.

Mark
  • 6,112
  • 4
  • 21
  • 46