Skip to content

Instantly share code, notes, and snippets.

@mitsuru793
Created November 30, 2015 10:25
Show Gist options
  • Select an option

  • Save mitsuru793/9e60f4ee8b4c7347437d to your computer and use it in GitHub Desktop.

Select an option

Save mitsuru793/9e60f4ee8b4c7347437d to your computer and use it in GitHub Desktop.
coffeeScriptでプライベート変数を使ってみる
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
// 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