Read more

CoffeeScript: How to instantiate a class with an attributes hash

Arne Hartherz
October 22, 2014Software engineer at makandra GmbH

This may be hard to find in the docs, but if you want CoffeeScript classes that instantiate their properties from a hash of attributes (like ActiveRecord), do it like this:

class User
  constructor: ({ @name, @email }) ->
    # empty contstructor is fine, CoffeeScript will do the magic.
Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

Example:

var batman = new User(name: 'Bruce Wayne', email: 'batman@example.com')
batman.name # => 'Bruce Wayne'
Posted by Arne Hartherz to makandra dev (2014-10-22 11:53)