Skip to content

Instantly share code, notes, and snippets.

@quangkeu95
Last active November 5, 2018 03:36
Show Gist options
  • Save quangkeu95/ea147739e2c2db6d561903b5b0babaea to your computer and use it in GitHub Desktop.
Save quangkeu95/ea147739e2c2db6d561903b5b0babaea to your computer and use it in GitHub Desktop.
Smart Setter
let employee = {
get workDuration() {
return this._workDuration;
},
set workDuration(value) {
if (value.length > 8) {
console.log("Work duration is too much, maximum 8 hours is allowed");
return;
}
this._workDuration = value;
}
};
employee.workDuration = 6;
console.log(employee.workDuration); // 6
employee.workDuration = 10; // Work duration is too much...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment