Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScriptだってプライベートがほしい

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 JavaScriptだってプライベートがほしい

Avatar for Kaneko Takeshi

Kaneko Takeshi

August 02, 2019
Tweet

More Decks by Kaneko Takeshi

Other Decks in Technology

Transcript

  1. 例) let Rectangle = class { constructor(height, width) { this.height

    = height; this.width = width; } calcArea() { return this.height * this.width; } };
  2. 例) class Engineer { constructer(name, type) { this.name = name;

    this.type = type; } speak() { console.log(`${this.name} is ${this.type} Engineer.`) } } const kaneko = new Engineer('Kaneko', 'Front End'); kaneko.speak(); / / Kaneko is Front End Engineer. kaneko.type = 'Unity'; kaneko.speak(); / / Kaneko is Unity Engineer.
  3. 例1) 運用でカバー 公開を意図しないフィールドには _ をつけるなど const Engineer = ((name, type)

    => { constructer(name, type) { this._name = name; this._type = type; } speak() { console.log(`${this._name} is ${this._type} Engineer.`) } })
  4. 例2)Soft Private const Engineer = ((name, type) => { const

    privateName = Symbol() const privateType = Symbol() return class EngineerClass { constructor(name, type) { this[privateName] = name; this[privateType] = type; } speak() { console.log(`${this[privateName]} is ${this[privateType]} Engineer.`) } } })()
  5. 例) class Engineer { #name = ''; #type = '';

    constructer(name, type) { this.#name = name; this.#type = type; } speak() { console.log(`${this.#name} is ${this.#type} Engneer.`) } } const kaneko = new Engineer('Kaneko', 'Front End'); kaneko.speak(); / / Kaneko is Front End Engineer. kaneko.#type = 'Unity'; / / SyntaxError kaneko.speak(); / / Kaneko is Front End Engineer.
  6. 例) class Engineer { private name = '' private type

    = '' constructer(name, type) { this.name = name; this.type = type; } } consta kaneko = new Engineer('kaneko', 'Front End'); kaneko.type = 'Unity'; / / public変数として定義される kaneko.speak(); / / Kaneko is Front End Engineer.