Share
Embed
Transcript
60 slides
-
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() -
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() UIJTOBNFୈҰҾ !UIJTͷ͜ͱɺলུՄೳ มͷຒΊࠐΈ DMBTTOFX'VODUJPOͯ͠ QSPUPUZQFΛ֦ு͢Δίʔυͱಉٛ TVQFSΫϥεͷࢀরՄೳ WBS͍Βͳ͍ ҾΛऔΔ߹ লུՄೳ GVODUJPO FYUFOETՄೳ -
var Animal, Horse, Snake, sam, tom, _ref, _ref1, __hasProp =
{}.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() { _ref = Snake.__super__.constructor.apply(this, arguments); return _ref; } Snake.prototype.move = function() { alert("Slithering..."); return Snake.__super__.move.call(this, 5); }; return Snake; })(Animal); Horse = (function(_super) { __extends(Horse, _super); function Horse() { _ref1 = Horse.__super__.constructor.apply(this, arguments); return _ref1; } 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(); ίϯύΠϧ͢Δͱ͜͏ͳΔ ͭ·Γ ϒϥβηʔϑʹ ॻ͜͏ͱ͢Δͱ͜͏ͳΔ ʂʂ͍ʂʂ -
Function square = (x) -> x * x cube =
(x) -> (square x) * x var cube, square; square = function(x) { return x * x; }; cube = function(x) { return square(x) * x; }; SFUVSO͞Εͯͳ͚Ε ࣗಈͰSFUVSO ϩʔΧϧม είʔϓઌ಄Ͱએݴ εϖʔεͷࠨଆ͕ؔ εϖʔεͷӈଆ͕Ҿ ͜ΕͰແ໊ؔ -
Function (Default) fill = (container, liquid = "coffee") -> "Filling
the #{container} with #{liquid}..." var fill; fill = function(container, liquid) { if (liquid == null) { liquid = "coffee"; } return "Filling the " + container + " with " + liquid + "..."; }; σϑΥϧτͷએݴ มͷຒΊࠐΈ͕Մೳ -
Function (Context) Account = (customer, cart) -> @customer = customer
@cart = cart $('.shopping_cart').bind 'click', (event) => @customer.purchase @cart ࣍ͷείʔϓͰ ಉ͡ίϯςΫετΛ༻͢Δ UIJT͕มΘΒͳ͍ UIBUUIJTͯ͠ UIBUΛ͏ͷͱಉ͡ॲཧ -
Function (Splats) gold = silver = rest = "unknown" awardMedals
= (first, second, others...) -> gold = first silver = second rest = others contenders = [ "Usain Bolt" "Asafa Powell" "Tyson Gay" "Michael Phelps" ] awardMedals contenders... alert "Gold: " + gold alert "Silver: " + silver alert "The Field: " + rest BSHVNFOUTΛΘͣ ෆಛఆͷҾΛड͚औΕΔ ͪΖΜͬͯྑ͍ NBQ૬ άϩʔόϧίϯςΫετͰBQQMZ -
Function (Splats) var awardMedals, contenders, gold, rest, silver, __slice =
[].slice; gold = silver = rest = "unknown"; awardMedals = function() { var first, others, second; first = arguments[0], second = arguments[1], others = 3 <= arguments.length ? __slice.call(arguments, 2) : []; gold = first; silver = second; return rest = others; }; contenders = ["Usain Bolt", "Asafa Powell", "Tyson Gay", "Michael Phelps"]; awardMedals.apply(null, contenders); alert("Gold: " + gold); alert("Silver: " + silver); alert("The Field: " + rest); BSHVNFOUT͕Ҏ্ͳΒ "SSBZQSPUPUZQFTMJDFΛDBMM άϩʔόϧίϯςΫετͰBQQMZ ͓·͚ࢀর -
Variable Safety outer = 1 changeNumbers = -> inner =
-1 outer = 10 inner = changeNumbers() var changeNumbers, inner, outer; outer = 1; changeNumbers = function() { var inner; inner = -1; return outer = 10; }; inner = changeNumbers(); ม໊ͷॏෳ ϩʔΧϧมԽ͢Δ -
Arrays bits = [ 1, 0, 1 0, 0, 1
1, 1, 0 ] numbers = [3..6] numbers = [3...6] var bits, numbers; bits = [1, 0, 1, 0, 0, 1, 1, 1, 0]; numbers = [3, 4, 5, 6]; numbers = [3, 4, 5]; ΧϯϚ͍Βͳ͍ -
Objects kids = brother: name: "Max" age: 11 sister: name:
"Ida" age: 9 kids = { brother: { name: "Max", age: 11 }, sister: { name: "Ida", age: 9 } }; ΧϯϚ\^͍Βͳ͍ ΠϯσϯτͰೖΕࢠΛදݱ -
Conditions var date, mood; if (singing) { mood = greatlyImproved;
} if (happy && knowsIt) { clapsHands(); chaChaCha(); } else { showIt(); } date = friday ? sue : jill; mood = greatlyImproved if singing if happy and knowsIt clapsHands() chaChaCha() else showIt() date = if friday then sue else jill ޙஔ ࡾ߲ԋࢉ -
Conditions (Chain) cholesterol = 127 healthy = 200 > cholesterol
> 60 var cholesterol, healthy; cholesterol = 127; healthy = (200 > cholesterol && cholesterol > 60); ͋Γͦ͏Ͱແ͔ͬͨ ൺֱԋࢉࢠͷνΣΠϯ -
Array Loops var i, item, _i, _j, _len, _len1, _ref,
_ref2; _ref = ['a', 'b', 'c']; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { item = _ref[i]; get(i + 1, item); } _ref1 = ['a', 'b', 'c']; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { item = _ref1[_j]; if (food !== 'c') { get(item); } } get i + 1, item for item, i in ['a', 'b', 'c'] get item for item in ['a', 'b', 'c'] when food isnt 'c' JO BSSBZ ͷ࣌ WBMVF JOEFY XIFOͰGPSจΛ੍ޚՄೳ -
Object Loops var age, ages, child, yearsOld; yearsOld = {
max: 10, ida: 9, tim: 11 }; ages = (function() { var _results; _results = []; for (child in yearsOld) { age = yearsOld[child]; _results.push("" + child + " is " + age); } return _results; })(); yearsOld = max: 10, ida: 9, tim: 11 ages = for child, age of yearsOld "#{child} is #{age}" PG PCKFDU ͷ࣌ LFZ WBMVF GPS͔ΒฦΓ͕Β͑Δ -
Closure Loops var filename, _fn, _i, _len; _fn = function(filename)
{ return fs.readFile(filename, function(err, contents) { return compile(filename, contents.toString()); }); }; for (_i = 0, _len = list.length; _i < _len; _i++) { filename = list[_i]; _fn(filename); } for filename in list do (filename) -> fs.readFile filename, (err, contents) -> compile filename, contents.toString() TIFMMϥΠΫʹGPS_EPͰ͖Δ ແ໊ؔͱ࣮ͯ͠ߦ -
Switch wheretogo = switch day when "Mon" then go work
when "Tue" then go relax when "Thu" then go iceFishing when "Fri", "Sat" if day is bingoDay go bingo go dancing when "Sun" then go church else go work TXJUDI͔ΒฦΓΛΒ͑Δ CSFBLෆཁ ҰߦͰॻ࣌͘UIFO ෳߦͳΒUIFO͍Βͳ͍ EFGBVMU͡Όͳͯ͘FMTF -
Operators is isnt not and or true, yes, on false,
no, off @, this of in :: @property, @.property unless hoge === !== ! && || true false this in prototype this.property if !hoge String::trim = ->ɹΈͨ͑͘Δ -
Operators (Exists?) if (typeof a != 'undefined' && typeof a.b
!= 'undefined' && typeof a.b.c !='undefined') { console.log a.b.c } else { console.log 'andefaindo!!!' } ͜Μͳܦݧ͋Γ·ͤΜ͔ -
Destructuring Assignment a = 1 b = 2 [a, b]
= [b, a] [a, b, c] = '0-1-2'.split '-' console.log a, b, c tag = "<impossible>" [open, contents..., close] = tag.split("") options = name: "geta6", age: 24, height: 178 {name, age, height} = options มͷೖΕସ͑ TQMJUͷฦΓΛׂΓͯ PQUJPOT͔Β LFZ໊Λ୳ࡧ class Animal constructor: (@name) -> ͜ͷײ͕֮௫ΊΔͱ!OBNFͰUIJTOBNFBSH ɹɹɹͷؾ࣋ͪѱ͕ܰ͞ݮ͢Δͱࢥ͍·͢ -
Raw Javascript (eval) hi = `function() { return [document.title, "Hello
JavaScript"].join(": "); }` ੜͷKBWBTDSJQU͑·͢ -
Block (String) mobyDick = "Call me Ishmael. Some years ago
-- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world..." html = """ <strong> cup of coffeescript </strong> """ ͜ͷϥΠϯΛΠϯσϯτج४ʹग़ྗ <strong>\n cup of coffeescript\n</strong> ʹͳΔ ్தվߦ҆શʹແࢹ͞ΕΔ -
Block (RegExp) OPERATOR = /// ^ ( ?: [-=]> #
function | [-+*/%<>&|^!?=]= # compound assign / compare | >>>=? # zero-fill right shift | ([-+:])\1 # doubles | ([&|<>])\2=? # logic / shift | \?\. # soak access | \.{2,3} # range or splat ) /// 3FH&YQʹίϝϯτ͕͚ͭΒΕΔ εϖʔεશͯແࢹ͞ΕΔͷͰaTΛ͏ -
Tips: Apply/Call B.FUIPEDBMM UIBU <BSH< BSH >> ୈҰҾʹίϯςΫετɺ ͭ·ΓB.FUIPE෦ͷUIJTΛࢦఆ͠·͢
ୈೋҾҎ߱ΧϯϚ۠ΓͰ B.FUIPEʹऔΒ͍ͤͨҾΛॻ͖·͢ ख࡞ۀͰؔΛݺͼग़͢ʹDBMM͕ศར -
Tips: Apply/Call B.FUIPEBQQMZ UIBU <BSHT> ୈҰҾʹίϯςΫετɺ ͭ·ΓB.FUIPE෦ͷUIJTΛࢦఆ͠·͢ ୈೋҾʹϦετͰɺ B.FUIPEʹऔΒ͍ͤͨҾΛॻ͖·͢
BSHVNFOUTͷΑ͏ͳϦετͷॲཧʹBQQMZ͕ศར -
Tips: Apply/Call class Test _logLevel = 0 echo: -> console.log.apply
console, arguments if @getLogLevel() > 1 getLogLevel: -> _logLevel setLogLevel: (lv) -> _logLevel = lv test = new Test test.echo 'Debug Message!!' ͜Μͳ෩ʹ͑ΔΑฤ -
Tips: Apply/Call class Hoge hoge: -> console.log 'hoge', @ instanceof
Hoge class Fuga fuga: -> console.log 'fuga', @ instanceof Hoge hoge = new Hoge fuga = new Fuga hoge.hoge() # hoge true fuga.fuga() # fuga false fuga.fuga.call hoge # fuga true )PHFͷΠϯελϯε͔νΣοΫ ίϯςΫετ͕IPHFͳͷͰUSVF ͭ͡ʹؾ࣋ͪѱ͍ ͜Μͳ͜ͱͰ͖ΔΑฤ -
Tips: Hoisting var a = 0; (function() { console.log(a); var
a = 2; }()); console.log(a); ͜͜ͰԿ͕දࣔ͞ΕΔ͔ ͜͜ʮʯ ˞KTͷείʔϓ୯ҐGVODUJPO -
var a = 0; (function() { console.log(a); var a =
2; }()); console.log(a); Tips: Hoisting -
Tips: Hoisting var a; a = 0; (function() { var
a; console.log(a); a = 2; }()); console.log(a); ͜͏ͳΔ ͜Ε͕ϗΠεςΟϯά KTͰ༠ൃ͞ΕΔҋ༷ $P⒎FFͳΒউखʹॲཧͯ͘͠ΕΔ