当前位置: 代码迷 >> JavaScript >> coffeescript+prototypejs使编撰nodejs更象ruby-like
  详细解决方案

coffeescript+prototypejs使编撰nodejs更象ruby-like

热度:781   发布时间:2012-10-26 10:30:59.0
coffeescript+prototypejs使编写nodejs更象ruby-like
首先安装prototype for nodejs
引用
npm install prototype

看个例子

prototype = require 'prototype'
Object.extend global, prototype

(9).times (x)->
  console.log x

[1,2,3,4].each (x)->
  console.log x


Person = Class.create #定义一个类
  initialize: (name)->
    @name = name
  say: (message)->
    "#{@name}:#{message}"

Pirate = Class.create Person, #继承父类
  say: ($super, message)->
    "#{$super message}, yarr!"

john = new Pirate 'Long John'

console.log john.say 'ahoy matey' #输出Long John:ahoy matey, yarr!


Class.create takes in an arbitrary number of arguments. The first―if it is another class―defines that the new class should inherit from it. All other arguments are added as instance methods; internally they are subsequent calls to addMethods (see below). This can conveniently be used for mixing in modules:

#define a module
Vulnerable =
  wound: (hp)->
    @health -= hp
    @kill() if @health < 0
  kill: ->
    @dead = true
#the first argument isn't a class object, so there is no inheritance ...
#simply mix in all the arguments as methods:
Person2 = Class.create Vulnerable,
  initialize: ->
    @health = 100
    @dead = false

bruce = new Person2
bruce.wound 55
console.log bruce.health #=> 45


coffeescript本身也自定义了类的另一种实现方式。
class Animal
  constructor: (@name) ->

  move: (meters) ->
    alert @name + " moved #{meters}m."

class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5

class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45

sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"

sam.move()
tom.move()

String::dasherize = ->
  this.replace /_/g, "-"


上面的代码转换成js后,前后对比,使用coffeescript代码量少了许多。我本身比较趋向于coffeescript方式声明类。

var Animal, Horse, Snake, sam, tom,
  __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__ = parent.prototype; return child; };

Animal = (function() {

  function Animal(name) {
    this.name = name;
  }

  Animal.prototype.move = function(meters) {
    return alert(this.name + (" moved " + meters + "m."));
  };

  return Animal;

})();

Snake = (function(_super) {

  __extends(Snake, _super);

  function Snake() {
    Snake.__super__.constructor.apply(this, arguments);
  }

  Snake.prototype.move = function() {
    alert("Slithering...");
    return Snake.__super__.move.call(this, 5);
  };

  return Snake;

})(Animal);

Horse = (function(_super) {

  __extends(Horse, _super);

  function Horse() {
    Horse.__super__.constructor.apply(this, arguments);
  }

  Horse.prototype.move = function() {
    alert("Galloping...");
    return Horse.__super__.move.call(this, 45);
  };

  return Horse;

})(Animal);

sam = new Snake("Sammy the Python");

tom = new Horse("Tommy the Palomino");

sam.move();

tom.move();


String.prototype.dasherize = function() {
  return this.replace(/_/g, "-");
};