HTML5 Zone is brought to you in partnership with:

A GNU/Linux g33k CLI+Web loving computer polyglot. Hemanth is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

CoffeeScript Classes

10.29.2012
| 1525 views |
  • submit to reddit

When Rails 3 was out I had a serious look at coffescript commit-diff but was not very much carried by it then.

But as the saying goes Real man code raw JS I always have great respect and love towards it, but the sugar and the kick this coffee is giving me is very high, especially with node.js

In this article I would like to express my <3 towards coffee classes.

There have been many arguments about why or why not to use coffeescript, but I feel it's a personal choice! Hence I shall not be considered the comparative aspect of raw JS and coffeescript, but shall be w.r.t to sugar that coffeescript has to offer.

Well before even proceeding further JavaScript also might get classes which also is under loads of arguments.

Simple class in coffeescript like :class Employee would get complied to :

(function() {
  var Employee;
  Employee = (function() {
    function Employee() {}
    return Employee;
  })();
}).call(this);

Yes, it's a lot of boilerplate, but if one is used to IIFEs this is very much readable and not an overhead.

Have a few more details :

class Employee
  constructor: (@name,@age,@empid) ->
 
  toString: -> "#{@name} : #{@age} : #{@empid}"
 
emp = new Employee "hemanth",2,420 
 
console.log emp.toString()

Will get compiled into :

(function() {
  var Employee, emp;
  Employee = (function() {
    function Employee(name, age, empid) {
      this.name = name;
      this.age = age;
      this.empid = empid;
    }
    Employee.prototype.toString = function() {
      return "" + this.name + " : " + this.age + " : " + this.empid;
    };
    return Employee;
  })();
  emp = new Employee("hemanth", 2, 420);
  console.log(emp.toString());
}).call(this);

Let's try some inheritence:

class Employees
  constructor: (@name,@age,@empid) ->
 
  toString: -> "#{@name} : #{@age} : #{@empid}"
 
emp = new Employee "hemanth",2,420 
 
console.log emp.toString()
 
 
class Boss extends Employees 
 
boss = new Boss "guru",2,840
 
console.log boss.toString

The above code would compile into the below, well yes looks heavy, but if one would write an effective JS, it would look like almost as below :

(function() {
  var Boss, Employees, boss, emp;
  var <strong>hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super</strong> = parent.prototype;
    return child;
  };
  Employees = (function() {
    function Employees(name, age, empid) {
      this.name = name;
      this.age = age;
      this.empid = empid;
    }
    Employees.prototype.toString = function() {
      return "" + this.name + " : " + this.age + " : " + this.empid;
    };
    return Employees;
  })();
  emp = new Employee("hemanth", 2, 420);
  console.log(emp.toString());
  Boss = (function() {
    <strong>extends(Boss, Employees);
    function Boss() {
      Boss.__super</strong>.constructor.apply(this, arguments);
    }
    return Boss;
  })();
  boss = new Boss("guru", 2, 840);
  console.log(boss.toString);
}).call(this);

Some more intreseting syntax sugar:

greet = ->(name) "Hello #{name}!"

Would look like :

(function() {
  var greet;
  greet = function() {
    return name("Hello " + name + "!");
  };
}).call(this);

Notice the FAT arrows

greet = =>(name) "Hello #{name}!"

Would look like :

(function() {
  var greet;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  greet = __bind(function() {
    return name("Hello " + name + "!");
  }, this);
}).call(this);

So that was just a high dose of caffeine! But in general Ruby/Python coders are potential coffee addicts, seems like I'm also one of them! Do let me know if you are...

 

 

 

Published at DZone with permission of Hemanth Madhavarao, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)