Skip to content

Instantly share code, notes, and snippets.

@ryuone
Created July 3, 2011 14:20
Show Gist options
  • Save ryuone/1062260 to your computer and use it in GitHub Desktop.
Save ryuone/1062260 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>coffee-script</title>
<script src='coffee-script.js' type='text/javascript'></script>
<script src='./sampleA.coffee' type='text/coffeescript'></script>
</head>
<body>
<div></div>
</body>
</html>
class Foo
constructor:(@name) ->
@foo = true
say: ->
@get_name()
map: ->
mA = [1..10].map (i) -> i*2
reduce: ->
mC = [1..10].reduce (t, s) -> t + s
some: ->
list = ["aaa", "bbb"]
tweet = "aaaa bbbb ccc"
list.some (word) ->
!!~tweet.indexOf " #{word} "
hit:(n) ->
return true if n is 3
return false
passed: ->
passed = []
failed = []
scores = [{score:100,name:'A'},
{score:80, name:'B'},
{score:60, name:'C'},
{score:40, name:'D'},
{score:20, name:'E'},
{score:0, name:'F'}]
(if s.score > 60 then passed else failed).push s.name for s in scores
passed
get_name: ->
@name if @name?
set_name: (@name) ->
execute_hit: ->
@hit?
class Bar extends Foo
constructor:(@name) ->
@bar = true
super
say: ->
@name
change_name: (name) ->
@set_name?(name)
splats: (aa, bb...) ->
console.log aa
console.log bb
foo = new Foo("Foo's Name")
bar = new Bar("Bar's Name")
console.log foo.say()
console.log bar.say()
console.log bar.map()
console.log bar.some()
bar.set_name("BarBar's Name")
console.log bar.get_name()
console.log bar.passed()
console.log bar.splats("1", "2", "3", "4")
//sampleA.coffeeをjsに変換した結果
(function() {
var Bar, Foo, bar, foo;
var __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;
}, __slice = Array.prototype.slice;
Foo = (function() {
function Foo(name) {
this.name = name;
this.foo = true;
}
Foo.prototype.say = function() {
return this.get_name();
};
Foo.prototype.map = function() {
var mA;
return mA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(function(i) {
return i * 2;
});
};
Foo.prototype.reduce = function() {
var mC;
return mC = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(function(t, s) {
return t + s;
});
};
Foo.prototype.some = function() {
var list, tweet;
list = ["aaa", "bbb"];
tweet = "aaaa bbbb ccc";
return list.some(function(word) {
return !!~tweet.indexOf(" " + word + " ");
});
};
Foo.prototype.hit = function(n) {
if (n === 3) {
return true;
}
return false;
};
Foo.prototype.passed = function() {
var failed, passed, s, scores, _i, _len;
passed = [];
failed = [];
scores = [
{
score: 100,
name: 'A'
}, {
score: 80,
name: 'B'
}, {
score: 60,
name: 'C'
}, {
score: 40,
name: 'D'
}, {
score: 20,
name: 'E'
}, {
score: 0,
name: 'F'
}
];
for (_i = 0, _len = scores.length; _i < _len; _i++) {
s = scores[_i];
(s.score > 60 ? passed : failed).push(s.name);
}
return passed;
};
Foo.prototype.get_name = function() {
if (this.name != null) {
return this.name;
}
};
Foo.prototype.set_name = function(name) {
this.name = name;
};
Foo.prototype.execute_hit = function() {
return this.hit != null;
};
return Foo;
})();
Bar = (function() {
__extends(Bar, Foo);
function Bar(name) {
this.name = name;
this.bar = true;
Bar.__super__.constructor.apply(this, arguments);
}
Bar.prototype.say = function() {
return this.name;
};
Bar.prototype.change_name = function(name) {
return typeof this.set_name === "function" ? this.set_name(name) : void 0;
};
Bar.prototype.splats = function() {
var aa, bb;
aa = arguments[0], bb = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
console.log(aa);
return console.log(bb);
};
return Bar;
})();
foo = new Foo("Foo's Name");
bar = new Bar("Bar's Name");
console.log(foo.say());
console.log(bar.say());
console.log(bar.map());
console.log(bar.some());
bar.set_name("BarBar's Name");
console.log(bar.get_name());
console.log(bar.passed());
console.log(bar.splats("1", "2", "3", "4"));
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment