Last active
February 22, 2018 23:22
-
-
Save jpenney1/09cf75c00ec9c002d243328939428821 to your computer and use it in GitHub Desktop.
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
var Oldsmobile = function(year, color) { | |
// private members: | |
var allowed = ['red', 'black', 'tan']; | |
var defaultColor = 'black'; | |
// manipulate instance initialization by accessing constructor methods: | |
Oldsmobile.addAllowed(allowed, 'blue'); | |
// privleged members: | |
this.color = allowed.find(_color => _color === color) ? color : defaultColor; | |
this.speed = 0; | |
this.drive = speed => this.speed = speed; | |
this.year = year; | |
}; | |
// public members: | |
Oldsmobile.prototype.customPaintjob = function(_color){this.color = _color}; | |
//constructor members: | |
Oldsmobile.addAllowed = (colors, newColor) => colors.push(newColor); | |
var myAntiqueCar = new Oldsmobile(1904, 'blue'); | |
myAntiqueCar.drive(10); | |
myAntiqueCar.customPaintjob('orange'); | |
myAntiqueCar; | |
// source: https://crockford.com/javascript/private.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment