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

首先安装prototype for nodejs
[quote]npm install prototype[/quote]
看个例子

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, "-");
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值