Created
November 7, 2016 17:59
-
-
Save ernestlv/a79994df8a29faad29bc3a7a4974c503 to your computer and use it in GitHub Desktop.
create private properties in ES6 class using weakmaps
This file contains 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
// taken from https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/ | |
let SimpleDate = (function() { | |
let _years = new WeakMap(); | |
let _months = new WeakMap(); | |
let _days = new WeakMap(); | |
class SimpleDate { | |
constructor(year, month, day) { | |
// Check that (year, month, day) is a valid date | |
// ... | |
// If it is, use it to initialize "this" date | |
_years.set(this, year); | |
_months.set(this, month); | |
_days.set(this, day); | |
} | |
addDays(nDays) { | |
// Increase "this" date by n days | |
// ... | |
} | |
getDay() { | |
return _days.get(this); | |
} | |
} | |
return SimpleDate; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment