首先安装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 + gulp Features 使用express generator的代码结构 使用ncg命令,创建项目模板 目录说明 src/ build/ start supervisor build/bin/www 当开发的时候,只需要 gulp 然后就交给gulp的watch来做 ...
标题 "NODEJS+NPM+COFFEESCRIPT" 暗示了这个压缩包可能包含关于使用Node.js、NPM(Node Package Manager)以及CoffeeScript的教程或项目资源。以下是关于这三个核心概念的详细解释和相关知识点: Node.js: Node.js...
而Sass(Syntactically Awesome Style Sheets)则是一种扩展了CSS的预处理器,它引入了变量、嵌套规则、混合(mixins)、函数等特性,使得CSS编写更易于维护和扩展。 在Ruby-on-Rails环境中,使用bootstrap-sass...
开始使用React + Redux + Immutable.js + CoffeeScript + Mocha + Webpack 概念 操作类型在src / Types.coffee中定义 为方便起见,动作生成器在src / actions /中定义 要触发动作,请调用state.dispatch(action) ...
以下是一个简单的Koa2+CoffeeScript+PostgreSQL应用的代码示例: ```coffeescript # 引入Koa2和pg require 'koa' { Pool } = require 'pg' # 初始化Koa2应用 app = new Koa() # 创建PostgreSQL连接池 pool = new ...
Phaser CoffeeScript + Browserify + LiveReload 开发样板 这是 Phaser 框架的一个非常简单的样板,包括的 Together 游戏示例。 安装 要轻松开始在 CoffeeScript 中使用 Phaser, git clone ...
《构建个人待办事项清单应用:Node.js+CoffeeScript+Express+Jade+MongoDB+mongoskin+jQuery+Underscore+Backbone》 在这个项目中,开发者使用了一系列先进的前端和后端技术来创建了一个名为"mytodo"的个人待办事项...
这个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代码。 如果您对此项目有任何想法,可以给我写信到或提出请求。
系统环境配置包括 Tomcat8.0 + JDK1.8 + Windows 10 + Ruby + Node.js,开发语言包括 JAVA、CoffeeScript、SCSS,数据库使用 MySQL,开发环境包括 Eclipse、Grunt。 系统的优势在于方便、简单、舒适、实用在线,...
Ruby-Torba是一个针对Sprockets的现代化资源管理器,旨在提供一个无需依赖Bower的解决方案。在Web开发中,资源管理对于组织和优化前端资产(如JavaScript、CSS、图片等)至关重要。Sprockets是Ruby on Rails框架中的...
CoffeeScript小书+CoffeeScript Coobook 中文版 - v1.1
它基于进行了一些小的更改: 添加对CoffeeScript和Pug的支持演示如何在App.svelte中使用CoffeeScript / Pug 更新了此自述文件添加了文件svelte.config.js(用于VS Code Svelte语言工具) 添加了文件launch.json...
使用火焰图调试 Node.js CPU 使用率 这是一个快速的操作指南,用于在您的 Node.js 应用程序中调试神秘的 CPU 使用情况。 它适用于 Linux 上的 Node 0.10,我认为没有太多记录,如果有的话。 这个想法是使用获取我们...
在本项目中,“modern-quickstart”是一个基于React.js、Coffeescript、CJSX、SCSS、Gulp和Browserify的现代化前端开发环境。这个框架为开发者提供了一个高效、快速的启动模板,用于创建功能丰富的Web应用程序。让...
注意:此编译器库已替换了用Ruby编写的原始CoffeeScript编译器。 依存关系 该库依赖于coffee-script-source gem,它在每次发布新版本的CoffeeScript时都会进行更新。 ( coffee-script-source gem的版本号会与每个...
Ruby-Sprockets是一个强大的基于Rack的资源打包系统,主要设计用于简化Web应用程序中的静态资源(如JavaScript、CSS和图像)的管理和优化。在Rails框架中,Sprockets被广泛使用,它允许开发者以一种模块化和高效的...
CoffeeScript是JavaScript的一个预处理器,它提供了一种更简练、易读的语法,使得编写JavaScript代码更加高效且减少错误。 1. **CoffeeScript的基本概念**: - CoffeeScript由Jeremy Ashkenas创建,它的设计目标是...
通过使用Coffeescript,开发者可以减少语法上的错误,同时享受到更接近于Python或Ruby的编程体验。 Express.js是Node.js中最受欢迎的Web应用框架,它简化了构建HTTP服务器的过程。Express提供了一系列强大的功能,...