Here’s an example of what I’m trying to do.
User = (name,dob,rank,score) ->
{
name: name
dob: dob
details: {
rank: rank
score:score
}
basicInfo: -> return "Name: #{@name} - Dob: #{@dob} - #{@details.rank} "
detailedInfo: -> return "Name: #{@name} - Rank: #{@details.rank} - Score: #{@details.score}"
}
User::displayName = ->
console.log @name
bob = new User("Bob","10/12/69",1,100000)
bob.displayName()
I’m getting an error that says “Uncaught TypeError: Object # has no method ‘displayName'”
Not really sure why I’m getting this error. Any help is greatly appreciated.
When you return a new object from the constructor, it doesn’t share the prototype. A proper constructor adds properties/methods to
this:You might want to take advantage of CoffeeScript’s
classabstraction, which just generates standard constructor functions:This is functionally the same as above, except that
basicInfoanddetailedInfohere are already in theprototype, where they should be. With this,User::displayName = -> console.log @nameshould work fine.See http://coffeescript.org/#classes