Browse Source

feat(animals): add more properties and methods to improve the exercise

vo
Michele Sessa 1 year ago committed by Michele
parent
commit
b3c3e3205a
  1. 24
      js/tests/animals_test.js
  2. 23
      subjects/animals/README.md

24
js/tests/animals_test.js

@ -106,6 +106,30 @@ t(({ eq }) => {
})
})
// Test Dodo
t(({ eq }) => {
let myDog = new Dog("Rex")
let myBird = new Bird()
let myDodo = new Dodo("Dill")
return eq(
{
name: myDog.name,
hasOwn: Object.hasOwn(myDog, 'name'),
name: myBird.name,
hasOwn: Object.hasOwn(myBird, 'name'),
name: myDodo.name,
hasOwn: Object.hasOwn(myDodo, 'name'),
},
{
name: "Rex",
hasOwn: true,
name: "Anonymous",
hasOwn: false,
name: "Dill",
hasOwn: true,
})
})
export const setup = () => {
}

23
subjects/animals/README.md

@ -8,14 +8,17 @@ In order to understand how `prototypal inheritance` works you will have to recre
You will have `Animal` which will an object and will have the following properties:
- `canEat`, `canBreath`, `isAlive`: All booleans, set to true.
- `name`: set to `"Anonymous"`.
- `WhoAmI`: Function that returns a string, here it will be `"I'm an animal"`.
- `NameToUppercase`: Function that returns `name` to uppercase.
The following objects will inherit from `Animal` and add/override fields as follow:
- `Dog`: adds property `canRun` set to `true`.
- `Bird`: adds properties `canFly` and `makesEggs` set to `true`.
- `Dodo`: overrides `canFly` and `isAlive` to `false`.
- `Dodo`: inherits from `Bird` and overrides `canFly` and `isAlive` to `false`.
All of them will override `WhoAmI`, returning `"I'm a [animal name]"`.
- All of them will override `WhoAmI`, returning `"I'm a [animal name]"`.
- All can accept a specific `name` as argument when created or use the default in `Animal` if no argument is provided.
> There are many different ways to work on prototypes, we suggest to use `Object.assign` and similar functions, but feel free to experiment other ways to understand the differences.
@ -27,8 +30,8 @@ Here is a possible program to test your function:
// Your implementation of Animal, Dog, Bird and Dodo ...
const myDog = new Dog()
const myBird = new Bird()
const myDodo = new Dodo()
const myBird = new Bird("Trill")
const myDodo = new Dodo("Mallone")
console.log("canEat: " + myDog.canEat + " | hasOwn: " + Object.hasOwn(myDog, "canEat"))
console.log("canEat: " + myBird.canEat + " | hasOwn: " + Object.hasOwn(myBird, "canEat"))
console.log("canEat: " + myDodo.canEat + " | hasOwn: " + Object.hasOwn(myDodo, "canEat"))
@ -39,6 +42,12 @@ console.log("makesEggs: " + myDodo.makesEggs + " | hasOwn: " + Object.hasOwn(myD
console.log("WhoAmI: " + myDog.WhoAmI() + " | hasOwn: " + Object.hasOwn(myDog, "WhoAmI"))
console.log("WhoAmI: " + myBird.WhoAmI() + " | hasOwn: " + Object.hasOwn(myBird, "WhoAmI"))
console.log("WhoAmI: " + myDodo.WhoAmI() + " | hasOwn: " + Object.hasOwn(myDodo, "WhoAmI"))
console.log("name: " + myDog.name + " | hasOwn: " + Object.hasOwn(myDog, "name"))
console.log("name: " + myBird.name + " | hasOwn: " + Object.hasOwn(myBird, "name"))
console.log("name: " + myDodo.name + " | hasOwn: " + Object.hasOwn(myDodo, "name"))
console.log("NameToUppercase: " + myDog.NameToUppercase() + " | hasOwn: " + Object.hasOwn(myDog, "NameToUppercase"))
console.log("NameToUppercase: " + myBird.NameToUppercase() + " | hasOwn: " + Object.hasOwn(myBird, "NameToUppercase"))
console.log("NameToUppercase: " + myDodo.NameToUppercase() + " | hasOwn: " + Object.hasOwn(myDodo, "NameToUppercase"))
```
And its output :
@ -55,6 +64,12 @@ makesEggs: true | hasOwn: true
WhoAmI: I'm a dog | hasOwn: true
WhoAmI: I'm a bird | hasOwn: true
WhoAmI: I'm a dodo | hasOwn: true
name: Anonymous | hasOwn: false
name: Trill | hasOwn: true
name: Mallone | hasOwn: true
NameToUppercase: ANONYMOUS | hasOwn: false
NameToUppercase: TRILL | hasOwn: false
NameToUppercase: MALLONE | hasOwn: false
$
```

Loading…
Cancel
Save