Created
November 30, 2015 10:25
-
-
Save mitsuru793/9e60f4ee8b4c7347437d to your computer and use it in GitHub Desktop.
coffeeScriptでプライベート変数を使ってみる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Car | |
| _gasoline = 100 | |
| constructor: (@name) -> | |
| run: -> | |
| _gasoline -= 1 | |
| console.log "#{@name}のガソリン:#{_gasoline}" | |
| track = new Car("トラック") | |
| f1 = new Car("F1") | |
| track.run() # トラックのガソリン:99 | |
| f1.run() # F1のガソリン:98 | |
| console.log(track._gasoline) # undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Generated by CoffeeScript 1.10.0 | |
| (function() { | |
| var Car, f1, track; | |
| Car = (function() { | |
| var _gasoline; | |
| _gasoline = 100; | |
| function Car(name) { | |
| this.name = name; | |
| } | |
| Car.prototype.run = function() { | |
| _gasoline -= 1; | |
| return console.log(this.name + "のガソリン:" + _gasoline); | |
| }; | |
| return Car; | |
| })(); | |
| track = new Car("トラック"); | |
| f1 = new Car("F1"); | |
| track.run(); | |
| f1.run(); | |
| console.log(track._gasoline); | |
| }).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment