Talk:CoffeeScript

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Software / Computing  (Rated Start-class, Low-importance)
WikiProject icon This article is within the scope of WikiProject Software, a collaborative effort to improve the coverage of software on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the project's quality scale.
 Low  This article has been rated as Low-importance on the project's importance scale.
Taskforce icon
This article is supported by WikiProject Computing (marked as Low-importance).
 

Rails[edit]

There have been reports that official support for CoffeeScript 
will be included in the next point release of Ruby on Rails.[4] 

'next' as in which version? Most of this article is not time-independent. as in, next year it will be wrong, and a year ago it was wrong. Say 'as of June 2010...' or something like that to qualify now-dependent statements. Think of someone reading this 5 or 10 years from now - your words will still be here.

OsamaBinLogin (talk) 17:26, 23 March 2011 (UTC)

  • Good point, but moot now that the official announcement has been made. Proper citation (a tweet by a Rails Core Team member, linking to a commit in the official Rails repository) included. --Trevor Burnham (talk) 15:55, 13 April 2011 (UTC)

Dialects, forks, derivatives[edit]

One should probably talk about Coco, a dialect of CoffeeScript, and furthermore LiveScript, a fork of Coco. — Preceding unsigned comment added by 82.150.248.29 (talk) 14:12, 25 July 2012 (UTC)

Example[edit]

The example of coffeescript isn't really mind blowing and doesn't show the advantages, disadvantages or construct differences of using CoffeeScript instead of JavaScript. Why not give a simple example of list comprehension or classes? — Preceding unsigned comment added by 81.206.220.44 (talk) 08:20, 2 January 2013 (UTC)

I agree that it is rather uninformative. website is far more informative than Wikipedia, and is actually itself relatively encyclopedic. I would say it is one of the most well documented languages I've seen. The home page has a bias, but it isn't much more significant than that of Wikipedia's Python article. It isn't good enough to copy and paste, though. It would be a fantastic reference to draw a lot of up-to-date information from. impinball (talk) 04:04, 29 July 2014 (UTC)

CoffeeScript Classes[edit]

CoffeeScript is prototype-based, just as JavaScript us (with a one-to-one correlation). It is also class-based, which isn't reflected at all on here. These two examples here are equivalent code.

Prototype-based:

Person = (name, age) -> # Constructor
  @name = name
  @age = age

Person::say = (sentence) -> # Instance method
  console.log "#{@name}: #{sentence}"

Person::sayAge = ->
  @say "My name is #{@name}"

Person::sayAge = ->
  @say "My age is #{@age}"

Person.getName = (person) -> # Static method
  person.name

Person.getAge = (person) ->
  person.age

Class-based:

class Person
  constructor: (name, age, gender) -> # Constructor
    @name = name
    @age = age
    @gender = gender

  say: (sentence) -> # Instance method
    console.log "#{name}: #{sentence}"

  sayName: ->
    @say "My name is #{@name}"

  sayAge: ->
    @say "My age is #{@age}"

  @getName: (person) -> # Static method
    person.name

  @getAge: (person) ->
    person.age

Inheritance is more complete than its common JavaScript counterpart foo.prototype = bar.prototype.

CoffeeScript

class Mother extends Person
  constructor: (info..., children) ->
    super info...
    @children = new Array([children])[0] # convert to Array

  sayChildrenNames: ->
    for child of @children
      [pronoun, relation] = if child.gender is 'male' then ['His', 'son'] else ['Her', 'daughter']
      @say "This is my #{relation} #{child.name}. #{pronoun} age is #{child.age}."

console.log Mother instanceof Person # true

JavaScript

function Mother() {
  var info = Array.prototype.slice.call(arguments);
  var children = info.pop();
  Person.call(this, args);
  this.children = new Array([children])[0]
}

Mother.prototype = Person.prototype;

Mother.prototype.sayChildrenNames = function () {
  var children = this.children;
  for (var i = children.length, child; i-- > 0; ) {
    var child = children[i];
    var pronoun, relation;
    if (child.gender === 'male') {
      pronoun = 'His';
      relation = 'son';
    } else {
      pronoun = 'Her';
      relation = 'daughter';
    }
    this.say('This is my ' + relation + ' ' + child.name + '. ' + pronoun +
             ' age is ' + child.age);
};

console.log(Mother instanceof Person); # false

Could we add a more complete description of classes in CoffeeScript? These specific examples aren't very encyclopedic, but this needs covered.

I would also like to add that it is starting to replace JavaScript. Github no longer has the "any more JavaScript must be written in CoffeeScript" in their guide. That link points to an exclusively CoffeeScript-oriented guide (even though it says "JavaScript" in the navigation bar). impinball (talk) 03:38, 29 July 2014 (UTC)