Created
March 26, 2019 22:46
-
-
Save area73/b2f7a1372855e683acc631591e890561 to your computer and use it in GitHub Desktop.
Private methods in ES6
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
// USING SYMBOL | |
'use strict' | |
const _resetTable = Symbol('resetTable'); | |
const _replaceTable = Symbol('replaceTable'); | |
; | |
class Rejs { | |
constructor() { | |
} | |
// public | |
createTable(tableName) { | |
this[_resetTable](tableName) | |
} | |
// private | |
[_replaceTable](tableName, data) { | |
// wadus wadus | |
} | |
} | |
// USING WeekMaps | |
const _counter = new WeakMap(); | |
const _action = new WeakMap(); | |
class Countdown { | |
constructor(counter, action) { | |
_counter.set(this, counter); | |
_action.set(this, action); | |
} | |
dec() { | |
let counter = _counter.get(this); | |
if (counter < 1) return; | |
counter--; | |
_counter.set(this, counter); | |
if (counter === 0) { | |
_action.get(this)(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment