LoginSignup
9
9

More than 5 years have passed since last update.

class構文でprivateなメソッドを定義する

Last updated at Posted at 2012-01-30
class Hoge
  private_ = ->
    console.log 'private'

  public_: ->
    private_.call(@)

private_はそのまま呼び出すのではなくcallapplyを使わないとprivateメソッドぽくならない。
コンパイルすると

var Hoge, hoge;

Hoge = (function() {
  var private_;

  function Hoge() {}

  private_ = function() {
    return console.log('private');
  };

  Hoge.prototype.public_ = function() {
    return private_.call(this);
  };

  return Hoge;
})();
9
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9