22 Mar 2012

Classes: introduction

First of all, I want to say several words about classes in CoffeeScript. In fact, any class here is just a map, where keys are field names and values are anything you want - numbers, strings, functions... Here is very simple example:
class Animal
  name: undefined
  @numberOfRegisteredAnimals: 0
  constructor: (@name) ->
    Animal.numberOfRegisteredAnimals += 1

  @printNumberOfRegistedAnimals: () ->
    console.log "The number of registered animals is equal to", 
  sayName: () ->
    console.log "My name is " + @name + ", " + @voice() 

class Cat extends Animal
  voice: () ->
    return "meow!!!"

class Dog extends Animal
  voice: () ->
    return "woof!!!"

cat = new Cat "Kuzya"
dog = new Dog "Sharik"

20 Mar 2012


Welcome dear reader!
I open this blog to share my experience of programming on CoffeeScript with NodeJS and doing other things.
As you probably know, CoffeeScript is a little language that compiles into JavaScript (as it is said here). As for NodeJS, it is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications (nodejs.org). There are tons of blogs, articles and tutorials devoted to both of them. Why do I open a new one?
Well, because I have very specific project that I work on now. I want to implement the transactional key-value store based on event-driven architecture. EDA is nice when you, well, want not to stop work while waiting for server response. However, when you want to write lots of data in one file you are in a trouble, because data in NodeJS is written asynchronously and the order of writing is not quarantined. Generally speaking, when you start working with EDA it firstly breaks your brain and then, if you are still alive, causes lots of problems and additional lines in your code.
The one more funny thing in my project is that I have complete database written on Java and need to repeat interfaces as much as possible. This causes even more problems. How to make function overload in the language where OOP is based on prototypes? How to make several constructors for one class? Why changing data in one class instance can affect other instances?
I will try to answer these questions and many others. I will not explain the very basics (you can easily find them yourself) except, maybe, the things that were kind of problem for me. And I hope to see your comments!