Skip to content

Instantly share code, notes, and snippets.

@jpenney1
Last active February 22, 2018 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpenney1/09cf75c00ec9c002d243328939428821 to your computer and use it in GitHub Desktop.
Save jpenney1/09cf75c00ec9c002d243328939428821 to your computer and use it in GitHub Desktop.
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