0

I am quite new to coffeescript, although I want to learn in order to optimize my workflow in the future.

The problem is that I am missing out some concepts, for example

var foo = {
    init: function() {
        this.ui.build();
        this.bindEvents(); 
    },
    bindEvents: function() {}
    ...
}

$('document').ready(function(){
    foo.init();
})

translated like this in coffeescript

foo = 
  init: ->
    @.ui.build();
    @.bindEvents();
  bindEvents: ->
  ...
  ...

$('document').ready(->
   foo.init();
)

What did I do wrong? What are your suggestions in my way of creating objects?

5
  • What makes you think you did something wrong? Commented Jun 9, 2013 at 18:35
  • because it says foo is not defined. Commented Jun 9, 2013 at 18:36
  • Seems to work fine for me: jsbin.com/ipugin/1/edit. Maybe the problem is in the code you didn't code. Commented Jun 9, 2013 at 18:41
  • Are your foo = ... and $(document).ready(...) in different files? Commented Jun 9, 2013 at 18:42
  • i got a problem in my code, my bad.. but the answer below suffice to answer my question. Commented Jun 9, 2013 at 18:51

1 Answer 1

1

Coffescript allows you to define classes.

class Foo
    constructor: ->
      @ui.build()
      @bindEvents()
    bindEvents: ->
    ...
    ...



$('document').ready () ->
   foo = new Foo

You shouldnt use @.field notation. Use @field instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.