首先安装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, "-");
};
分享到:
相关推荐
我的待办事项清单 Nodejs+CoffeeScript+Express+Jade+MongoDB+mongoskin+jQuery+Underscore+Backbone 编写 您可以访问: :
nodejs + coffeescript + gulp Features 使用express generator的代码结构 使用ncg命令,创建项目模板 目录说明 src/ build/ start supervisor build/bin/www 当开发的时候,只需要 gulp 然后就交给gulp的watch来做 ...
NODEJS NPM COFFEESCRIPT
开始使用React + Redux + Immutable.js + CoffeeScript + Mocha + Webpack 概念 操作类型在src / Types.coffee中定义 为方便起见,动作生成器在src / actions /中定义 要触发动作,请调用state.dispatch(action) ...
Phaser CoffeeScript + Browserify + LiveReload 开发样板 这是 Phaser 框架的一个非常简单的样板,包括的 Together 游戏示例。 安装 要轻松开始在 CoffeeScript 中使用 Phaser, git clone ...
这个demo是使用cocos2d-js+CoffeeScript+Browersify编写,主要是尝试如何在cocos2d-js中使用模块化的编程。 源码编译步骤: 1.安装CoffeeScript npm install -g coffee-script 2.安装Browersify npm install -g ...
Jade风味Ractive模板+ CoffeeScript + LESS + Webpack和Gulp的入门项目 特征 Jade对独立模板和Ractive模板的支持 带有Sourcemaps的CoffeeScript编译 LESS样式表支持 脚本和Ractive模板已经过优化,并与Webpack捆绑...
grunt-template-coffee-requirejs Grunt模板项目CoffeeScript + RequireJS ... 输出应用程序文件为app.js。 使用杏仁来运行requirejs代码。 如果您对此项目有任何想法,可以给我写信到或提出请求。
基于Koa2-CoffeeScript-PostgreSQL的服务器开发示例
CoffeeScript小书+CoffeeScript Coobook 中文版 - v1.1
使用火焰图调试 Node.js CPU 使用率 这是一个快速的操作指南,用于在您的 Node.js 应用程序中调试神秘的 CPU 使用情况。 它适用于 Linux 上的 Node 0.10,我认为没有太多记录,如果有的话。 这个想法是使用获取我们...
它基于进行了一些小的更改: 添加对CoffeeScript和Pug的支持演示如何在App.svelte中使用CoffeeScript / Pug 更新了此自述文件添加了文件svelte.config.js(用于VS Code Svelte语言工具) 添加了文件launch.json...
注意:此编译器库已替换了用Ruby编写的原始CoffeeScript编译器。 依存关系 该库依赖于coffee-script-source gem,它在每次发布新版本的CoffeeScript时都会进行更新。 ( coffee-script-source gem的版本号会与每个...
现代快速入门 使用 React.js 和 Coffeescript 的框架前端项目。 客户端前端 服务器
The Little Book on CoffeeScript 中文+英文版
jar包,官方版本,自测可用
因为 CoffeeScript 会将类似 Ruby 语法的代码编译成 JavaScript,而且大部分结构都相似,但不同的是 CoffeeScript 拥有更严格的语法。 语法:从 Lisp 系重生CoffeeScript打蛇打到七寸了. 它的最大功绩, 就是将 ...
concrete, 使用NodeJS和CoffeeScript编写的简单 持续集成 服务器 混凝土混凝土是一个简单的持续集成 服务器。 我没有足够的时间维护,所以我要寻找那些愿意贡献自己的人。 如果你想为新特性。问题或者请求请求提供...
Nodejs包,含coffeescript、express等 解压设置环境变量就可直接使用 非常方便
准备npm安装npm运行读取npm run结果笔记在每个测试文件中都需要src文件。 需要在每个测试文件中断电。 (在每个src文件中编写global.Foo = Foo;如果您希望像使用浏览器一样对其进行测试)