Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.
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;
})();
yuku_t
FLYWHEEL社でソフトウェアエンジニアをしています。昔はIncrements社最初の従業員としてQiitaを開発したりCTOやったりしていました。
http://yuku.takahashi.coffee
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away