0

I've a person.coffee file that contains the following code

class Person 
  constructor: (@name) ->
  talk: ->
    "hello"

module.exports = Person

Now I am trying to use it in app.js

Person = require "./person"
p = new Person "Emma"
console.log p.talk

It prints [Function] in the console. Any idea that what is wrong

Note: I've updated the spaces. Solution: I changed p.talk to p.talk() in app.js and its fixed now.

1
  • 2
    The indention is messed up. Have constructor and talk equally indented. Commented Sep 18, 2013 at 8:00

1 Answer 1

4

seems like your indent is broken, your code will compile to

var Person;

Person = (function() {

  Person.name = 'Person';

  function Person(name) {
    this.name = name;
    ({
      talk: function() {
        return "hello";
      }
    });
  }

  return Person;

})();

while you want something like this

class Person 
  constructor: (@name) ->

  talk: -> "hello"

which will be compiled into

var Person;

Person = (function() {

  Person.name = 'Person';

  function Person(name) {
    this.name = name;
  }

  Person.prototype.talk = function() {
    return "hello";
  };

  return Person;

})();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you. I modified the indentation but now its printing [Function] rather than "hello" any idea?
Okay I added () at the end of talk in app.js and its working now.

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.