Last active
November 24, 2019 21:22
-
-
Save jsmanifest/8e150cfd40fcbf23f822e7a2e5efaf8c to your computer and use it in GitHub Desktop.
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
class Frog { | |
constructor(name, gender, eyes, legs, scent, tongue, heart, weight, height) { | |
if (!Array.isArray(legs)) { | |
throw new Error('Parameter "legs" is not an array') | |
} | |
// Ensure that the first character is always capitalized | |
this.name = name.charAt(0).toUpperCase() + name.slice(1) | |
this.gender = gender | |
// We are allowing the caller to pass in an array where the first index is the left eye and the 2nd is the right | |
// This is for convenience to make it easier for them. | |
// Or they can just pass in the eyes using the correct format if they want to | |
// We must transform it into the object format if they chose the array approach | |
// because some internal API uses this format | |
this.eyes = Array.isArray(eyes) ? { left: eye[0], right: eye[1] } : eyes | |
this.legs = legs | |
this.scent = scent | |
// Pretending some internal API changed the field name of the frog's tongue from "tongueWidth" to "width" | |
// Check for old implementation and migrate them to the new field name | |
const isOld = 'tongueWidth' in tongue | |
if (isOld) { | |
const newTongue = { ...tongue } | |
delete newTongue['tongueWidth'] | |
newTongue.width = tongue.width | |
this.tongue = newTongue | |
} else { | |
this.tongue = newTongue | |
} | |
this.heart = heart | |
if (typeof weight !== 'undefined') { | |
this.weight = weight | |
} | |
if (typeof height !== 'undefined') { | |
this.height = height | |
} | |
} | |
} | |
const larry = new Frog( | |
'larry', | |
'male', | |
[{ volume: 1.1 }, { volume: 1.12 }], | |
[{ size: 'small' }, { size: 'small' }, { size: 'small' }, { size: 'small' }], | |
'sweaty socks', | |
{ tongueWidth: 18, color: 'dark red', type: 'round' }, | |
{ rate: 22 }, | |
6, | |
3.5, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment